首页 > 编程语言 > 详细

spring mvc $.ajax没有指定contentType ,导致后台无法接收到数据

时间:2018-08-07 01:20:06      阅读:232      评论:0      收藏:0      [点我收藏+]
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中

spring mvc $.ajax没有指定contentType ,导致后台无法接收到数据

原文:https://www.cnblogs.com/dongzhuangdian/p/9434232.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!