Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported

编程知识 更新时间:2023-04-05 07:00:35

已知后端post请求:

 

报错:

HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported

解决:

增加:

,contentType: "application/json;charset=UTF-8"

 

 

 

报错:

org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Unrecognized token 'id': was expecting ('true', 'false' or 'null'); nested exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'id': was expecting ('true', 'false' or 'null')

 

解决:

data:JSON.stringify(数据)

 

 

=======================================================

总结:

Caused by: com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'name': was expecting ('true', 'false' or 'null')

Caused by: com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'name': was expecting ('true', 'false' or 'null')

参数有问题,不能解析成json对象

ajax提交添加下面两行代码

contentType:'application/json;charset=utf-8'
data:JSON.stringify(数据)

 

var allData = {
          name:"张三",
          age:20
         };
$.ajax({
            type: "POST",
            url: "xxxx",
            contentType:'application/json;charset=utf-8',
            data:JSON.stringify(allData),
            success: function (data) {
                alert(data);
            }
        });

 

注意:

后端如果用springMVC的@RequestBody注解的话,则只能Json对象的字符串,不能接收Json对象,用 JSON.stringify(data)的方式将对象变成字符串,同时ajax请求也要指定dataType: "json",contentType:"application/json" ,这样就能用@RequestBody注解绑定对象或者List集合.

 

 

 

更多推荐

Content type 'application/x-www-form-urlencoded;charset=UTF-8' not sup

本文发布于:2023-04-05 07:00:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/3ffe19e1af3b82b38f896cadbac81005.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:application   www   Content   type   form

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!

  • 45423文章数
  • 14阅读数
  • 0评论数