spring mvc接受页面json对象,处理后传出json到页面并输出
JSON.stringify 必须要用,否则页面的json被spring mvc接受时候为对象,对面会自动转成对象字符串乱码。 所以用这个方法可以把json转成字符串传送。
页面
引用
<script src="${ctx}/common/js/customer/jquery.json-2.3.min.js"></script>
<div id="res"></div>
<script>
var postData=JSON.stringify( {"Body":{"SecUser":"iphone1.0"},"Header":{"MsaleTunn":"001","Sign":"4027ac80ca9bb07f638afd911c3febd9"}});
$.ajax({
url:"${ctx}/security/login",
type:"POST",
contentType: "application/json",
dataType: "json",
data:postData,
success:function(respon){
$("#res").html(JSON.stringify(respon));
},
error:function(er){
$("#res").html(JSON.stringify(er));
}
});
</script>
代码
@Controller
@RequestMapping("/security")
public class SecurityController {
@Autowired
private LoginAction loginAction;
@RequestMapping(value = "/login", method = RequestMethod.POST)
public @ResponseBody String login(@RequestBody String json) {
return loginAction.action(json);
}
@RequestMapping(value = "")
public String login() {
int i=1+1;
return "mobile/test";
}
}