StringMVC下json传值问题

来源:互联网 发布:nginx 400 错误 编辑:程序博客网 时间:2024/04/28 10:46

在springmvc下用ajax发送datagrid更新后的一条数据时发现后台一直接收不到。

var rows = datagrid.datagrid('getChanges','updated');$.ajax({    url:'${pageContext.request.contextPath}/professional/doEdit',    data:rows,    type:"get",    dataType:"json",    success:function(msg){            $.messager.alert("消息",msg);                                 }});

上网查了以后发现在后台接受数据的时候要加@RequestBody注解

@ResponseBody@RequestMapping("/doEdit")public Integer doEdit(@RequestBody Professional professional){    int result=service.edit(professional);    return result;}

但是报400错误,发现不应该用get方式提交,用get会追加到url上,后改成post;得到一个未知的object对象,获得professional属性是undefined。
又看了别人的解决方式,发现需要将datagrid更新的json对象转为字符串,同时要把两个中括号 [ ] 去除,再加上头部声明。

var rows = datagrid.datagrid('getChanges','updated');var rowstr=JSON.stringify(rows);$.ajax({    url:'${pageContext.request.contextPath}/professional/doEdit',    data:rowstr.substring(1,rowstr.length-1),//去[],否则后台无法接受对象    type:"post",    dataType:"json",    contentType:"application/json; charset=utf-8",//不加头部申明会报415错误    success:function(msg){            $.messager.alert("消息",msg);                                 }});

参考:
http://blog.csdn.net/loveme888/article/details/50009693
http://what-is-javascript.iteye.com/blog/1735691
至此完结。

0 0