標籤:關於java 與 ajax之間調用的一些總結
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();
}
關於java 與 ajax之間調用的一些總結