spring MVCå¨æ¥æ¶éå请æ±åæ°æ¶ï¼éè¦å¨Controlleræ¹æ³çéååæ°éåæ·»å @RequestBodyï¼è@RequestBodyé»è®¤æ¥æ¶çenctype (MIMEç¼ç )æ¯application/jsonï¼å æ¤åéPOST请æ±æ¶éè¦è®¾ç½®è¯·æ±æ¥æ头信æ¯ï¼å¦åSpring MVCå¨è§£æéå请æ±åæ°æ¶ä¸ä¼èªå¨ç转æ¢æJSONæ°æ®å解ææç¸åºçéåã
JS代ç ï¼
[javascript] view plain copy
var userList = new Array();
userList.push({name: "æå",pwd: "123"});
userList.push({name: "å¼ ä¸",pwd: "332"});
$.ajax({
type: "POST",
url: "/catalog/saveUsers",
data: JSON.stringify(userList),//å°å¯¹è±¡åºååæJSON
å符串dataType:"json",
contentType : 'application/json;charset=utf-8', //设置请æ±å¤´ä¿¡æ¯
success: function(data){
â¦
},
error: function(res){
â¦
}
});
Java代ç ï¼
[java] view plain copy
@Controller
<pre name="code" class="java">@RequestMapping("/catalog.do")
public class CatalogController {
@RequestMapping(params = "fn=saveUsers")
@ResponseBody
public AjaxJson saveUsers(@RequestBody List<User> userList) {
â¦
}
}
å¦ææ³è¦æ¥æ¶User[]æ°ç»ï¼åªéè¦æsaveUsersçåæ°ç±»åæ¹ä¸º@RequestBody User[] userArrayå°±è¡äºã
使ç¨$.ajax()æ¯å®å
¨å¯ä»¥éè¿çï¼ä½æ¯å¨EasyUIéæä¹ä¿®æ¹datagridçcontent-typeå¢ï¼
æ¥æ¾apiä¹æ¾ä¸å°ç¸å
³çå±æ§å¯ä»¥è®¾ç½®,ç¿»çeasyUIç
æºä»£ç æ¶æ¾å°loaderçæºä»£ç ï¼
[javascript] view plain copy
loader: function(_629, _62a, _62b) {
var opts = $(this).datagrid("options");
if(!opts.url) {
return false;
}
$.ajax({
type: opts.method,
url: opts.url,
data: _629,
dataType: "json",
contentType: opts.contentType?opts.contentType:"text/html",
success: function(data) {
_62a(data);
},
error: function() {
_62b.apply(this, arguments);
}
});
}
è¿éçå°loaderé没ææä¾ä¿®æ¹content-typeçé项ï¼è¿æ¶åå°±éè¦æ们èªå·±åloaderäºãJS代ç å¦ä¸ï¼
[javascript] view plain copy
$("#grid").datagrid({
loader : function(param, success, error) {
$.ajax({
type : opts.method,
url : opts.url,
dataType : 'json',
contentType : 'application/json;charset=utf-8', // 设置请æ±å¤´ä¿¡æ¯
data : JSON.stringify(param),
success : function(result) {
success(data);
}
});
}
});
好äºï¼ç°å¨å¨æ交çæ¶åãcontent-typeæ¯application/json;charset=utf-8ã