JSON parse error或者Content type ‘application/x-www-form-urlencoded;charset=UTF-8‘ not supported

Controller层写法:参数是实体类 参数注解为@RequestBody

public Object add(@RequestBody Entity entity){

......

......

......

......

}

实体类:


public class Entity{


private String name;

private String password;

//省略 getter和setter

}

原因:前台传参格式不是json格式,导致转换错误

 

解决:前端ajax请求,先将参数转为json格式,然后请求头contentType改为'application/json;charset=utf-8'

var allData = {

//参数名必须和实体属性名相同

"name":nameInput, //注意 name必须和实体属性名相同

"password":passwordInput, //password 必须和实体属性名相同

};


$.ajax({

type: 'get',

url: '',//接口地址

contentType:'application/json;charset=utf-8', //重点

data : JSON.stringify(allData), //重点

dataType: 'json',

async:false,

success: function(data){

//成功逻辑

},

error:function() {

//错误逻辑

}

});

 

你可能感兴趣的:(JSON parse error或者Content type ‘application/x-www-form-urlencoded;charset=UTF-8‘ not supported)