一般,直接传HashMap格式就可以了。
@RequestMapping(value = {"addsavePaper2"}) @ResponseBody public HashMap<String, String> addsave2(@RequestBody String paper, Model model) { HashMap<String, String> map = new HashMap<String, String>(); map.put("code", "0"); map.put("info", "2"); return map; }
但是如果map的值是中文可以能传不过去!!!
所以可以存在对象里面,注意如果对象里面没有中文,也可以直接发给前端,如果有中文的话,
可以先转为json然后toString, 以string的形式传过来。
@RequestMapping(value = {"addsaveAQuestion2"}) @ResponseBody public String addsaveQuestion2(@RequestBody String body, Model model) { RO ro = new RO(); ro.setCode("hhh"); ro.setInfo("sdf------返回"); System.out.println("------返回:" + ro); System.out.println("data:" + ro); JSONObject json=JSONObject.fromObject(ro); return json.toString(); }
上面用的json包是:net.sf.json.JSONArray
下面给以json 互传的例子:
//保存 function save(obj){ //保存并提交问题 var paperid = $("#Mytheme").data("paperid"); //问卷id var qid = $(obj).parents(".problems-item").index(); //问题顺序id var problem_content = $(obj).parents(".problems-item").find(".problem-tittle").children("input").val(); console.log("问题:" + problem_content); var ismust = $(obj).parents(".problems-item").find("#ismust").val(); var type = $(obj).parents(".problems-item").data("type"); //单选or多选 var min_num =$(obj).parents(".problems-item").find("#min_num").val(); var max_num =$(obj).parents(".problems-item").find("#max_num").val(); var option = $(obj).parents(".problems-item").find(".options-item"); var d = {}; //存放选项 option.each(function(){ name = $(this).index(); value = $(this).find(".opvalue").val(); d[name] = value; }); //传值 $.ajax({ type : "POST", url : '${ctx}/paper2/addsaveAQuestion', data : JSON.stringify({ //"paperid":paperid, "paperid": ${ paper.id }, "qid":qid, "option":JSON.stringify(d), "content":problem_content, "ismust":ismust, "type":type, "min_num":min_num, "max_num":max_num }), contentType : "application/json", cache : false, success : function(data){ //alert("测试返回值"); //alert(data); data =JSON.parse(data); //alert(data); if (data.code == 0) { alert("提交成功"); msg('提交成功~'); } else { alert("提交失败:" + data.info); msg('提交失败:' + data.info); } } }); };
@RequestMapping(value = {"addsaveAQuestion"}) @ResponseBody public String addsaveQuestion(@RequestBody String body) { JSONObject jsonObject = JSONObject.fromObject(body); String msg = ""; // 提取题目: Question2 que = new Question2(); try { que.setQid(Integer.valueOf(jsonObject.getString("qid"))); que.setPaperId(Integer.valueOf(jsonObject.getString("paperid"))); que.setContent(jsonObject.getString("content")); que.setLevel(UserUtils.getSysLevel()); que.setIsmust(Integer.valueOf(jsonObject.getString("ismust"))); que.setType(Integer.valueOf(jsonObject.getString("type"))); que.setMinNum(Integer.valueOf(jsonObject.getString("min_num"))); que.setMaxNum(Integer.valueOf(jsonObject.getString("max_num"))); } catch (Exception e) { // TODO: handle exception msg = "题目数据不全"; } // 提取选项 String option = jsonObject.getString("option"); JSONObject job2 = JSONObject.fromObject(option); int len = job2.size(); List<PaperOption2> oplist = new ArrayList<PaperOption2>(); for(int i = 0; i < len; i++) { PaperOption2 op = new PaperOption2(); try { op.setContent(job2.getString(i + "")); // 提取选项的内容 } catch (Exception e) { // TODO: handle exception msg = "选项不能为空"; } op.setOrderid(i); oplist.add(op); } // System.out.println("oplist: " + oplist); // 添加题目和选型到数据库: paperService.addQues(que, oplist); // 添加题目 RO ro = new RO(); if(!"".equals(msg)) { // 添加问题出错了 ro.setCode("1"); ro.setInfo(msg); } else { ro.setCode("0"); ro.setInfo("提交成功!"); } JSONObject jObject = JSONObject.fromObject( ro ); System.out.println("返回数据:" + jObject); return jObject.toString(); }