spring mvc jquery ajax 回发的几种方式

一。前台ajax json,通过url + 参数的方式 ,后台直接定义参数名称

$.ajax({
url:"dosomething?ids="+item_selected,
type : "post",
dataType : "json",
contentType : "application/json;charset=utf-8",
statusCode : {
/*
* 404 : function() { alert("page not found"); }, 200 :
* function() { alert("ok"); }, 302 : function(e) {
* alert(e); alert("jump"); }
*/
},
success : function(data) {

if (data.state == "success") {

} else {

}
},
error : function(XMLHttpRequest, textStatus, errorThrown) {


},
beforeSend : function() {

},
complete : function() {

}
});

后台代码:函数入参ids和前台url的参数ids一致

@RequestMapping("dosomething")
    @Transactional
    public @ResponseBody AjaxResult dosomething(Model model, String[] ids) {
        
        AjaxResult result = new AjaxResult("success", "注销成功!");
        return result;
    }

 

二。前台post参数,代码:

$.ajax({
url:"dosomething",
data : JSON.stringify({id:11,name:22}),
type : "post",
dataType : "json",
contentType : "application/json;charset=utf-8",
statusCode : {
/*
* 404 : function() { alert("page not found"); }, 200 :
* function() { alert("ok"); }, 302 : function(e) {
* alert(e); alert("jump"); }
*/
},
success : function(data) {

if (data.state == "success") {

} else {

}
},
error : function(XMLHttpRequest, textStatus, errorThrown) {


},
beforeSend : function() {

},
complete : function() {

}
});

后台代码:

public @ResponseBody AjaxResult cancel(Model model, @RequestBody List<String> ids) {
        
        AjaxResult result = new AjaxResult("success", "注销成功!");
        return result;
    }

如果后台的是 List<String> 类型的入参,前台的json对象直接就是 javascript 的数组格式 ,如["233","aaa","AAAAbb"],但是仍然需要使用JSON.stringify处理一下。

如果后台是普通对象的入参,前台的json对象就是{id:1111,name:0090}的格式,仍然也用JSON.stringify处理一下

三。还有一种方式

 

 

 

 

 

 

参考文章:

https://www.cnblogs.com/liuwt365/p/7750888.html

https://www.cnblogs.com/jym-sunshine/p/6141983.html

https://blog.csdn.net/u014656173/article/details/77073753

posted @ 2018-07-20 23:14  八方鱼  阅读(278)  评论(0)    收藏  举报