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

已知后端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集合.

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值