java 返回的是一个对象,dataType : "text";
java返回的是一个json字符串,dataType : "json"; 页面JSON.parse()解析,把json字符串解析成json对象
2,
contentType : "application/json", //发送给服务器的格式
dataType : "text", //服务器传给页面的格式,如果json的格式用JSON.parse()解析,如果是一个对象就text
3.
$.ajax({
url : "/test.do",
data : {‘x‘:‘001‘},
async : false,
type : "GET",
/* dataType : "text", */
contentType : "application/json",
dataType : "json",
success : function(data) {
alert(data[‘count‘]);
}
});
@RequestMapping(value = "test.do")
@ResponseBody
public Map<String, Object> test(HttpServletRequest request, String x) {
Map<String, Object> m = new HashMap<String, Object>();
m.put("count", 0);
return m;
}
4.
$.ajax({
url:"/test1.do",
data:{},
async:false,
type:"GET",
dataType:"json",
success:function(data){
var dataObj = JSON.parse(data);
alert(dataObj.1);
}
});
@RequestMapping(value = "test1.do")
@ResponseBody
public String checkInfo(HttpServletRequest request,HttpServletResponse response) {
Map<String, Boolean> checkMap = new HashMap<String, Boolean>();
checkMap.put("1",false);
checkMap.put("2",true);
JSONArray json = JSONArray.fromObject(checkMap);
return json.toString();
}
原文地址:http://7408089.blog.51cto.com/7398089/1632149