spring mvc $.ajax没有指定contentType ,导致后台无法接收到数据
var formData = JSON.stringify(this.rows); //将表单中的数据转为字符串
$.ajax({ type: "post", url: 'http://localhost:8080/data', data:formData, dataType:"json", contentType: "application/json;charset=utf-8", success: function (data) { } }); //开始漏掉了标红的属性设置,导致后台始终无法解析数据,能收到数据,但是很有乱七八糟的字符。
$.ajax http://www.runoob.com/jquery/ajax-ajax.html 有很多属性可以设置
@RequestMapping(value = "/data",method = RequestMethod.POST) public void getData(@RequestBody String str) throws IOException{ org.codehaus.jackson.map.ObjectMapper mapper = new org.codehaus.jackson.map.ObjectMapper(); JavaType javaType = mapper.getTypeFactory().constructParametricType(ArrayList.class, TestModel.class); List<TestModel> list = (List<TestModel>)mapper.readValue(str, javaType); System.out.println(str); System.out.println(list); }
还有一个疑问未解决:
@RequestMapping(value = "/data",method = RequestMethod.POST) public void getData(@RequestBody LIst<TestModel> list)
无法接收到数据到list中
作者:小德cyj
出处:http://www.cnblogs.com/dongzhuangdian
欢迎转载,希望注明出处