参考:http://www.cnblogs.com/andyfengzp/p/6184752.htmlhtml
1. 传json 对象!
$.ajax({
url :url2,
type: 'post',
dataType : "json",
data : {id:'22',remarks:'aaa'},
async : false, // 同步请求
success : function(data) {
debugger ;
}
});ajax后台spring
@RequestMapping(value = "f2")
public String f2( User u,HttpServletRequest request, HttpServletResponse response) {
return "f1";
}json
2.传 json字符串
$.ajax({
url :'${ctx}/test/testData/f1',
type: 'post',
dataType : "json",
contentType:'application/json;charset=UTF-8',//关键是要加上这行
// data : {id:'22',remarks:'aaa'},
data : JSON.stringify( {id:'22',remarks:'aaa'}),
async : false, // 同步请求
success : function(data) {
debugger ;
}
});mvc@RequestMapping(value = "f1")
public String f1( @RequestBody User u,HttpServletRequest request, HttpServletResponse response) {
return "f1";
}app
3. springmvc 接受 map
后台 : 必须是@requestBody 这样接收, 否则,map会当成Model 类处理async
@RequestMapping(value = "f3")
public String f3( @RequestBody Map<String,String> map,HttpServletRequest request, HttpServletResponse response) {
String string = map.get("remarks");
return "f1";
}post前台数据格式: json字符串 , 带上contentType:'application/json;charset=UTF-8',url
$.ajax({
url :'${ctx}/test/testData/f3',
type: 'post',
dataType : "json",
contentType:'application/json;charset=UTF-8',//关键是要加上这行
// data : {id:'22',remarks:'aaa'},
data : JSON.stringify( {id:'22',remarks:'aaa'}),
async : false, // 同步请求
success : function(data) {
debugger ;
}
});spa