ajax 请求 Failed to load resource: the server responded with a status of 415 (Unsupported Media Type) 和 400 error
//开始组装 数组
var list = new Array();
var json = "[" +
"{" +
"\"id\":1," +
"\"name\":\"唐山\"," +
"\"person.idType\":\"1\"" +
"}," +
"{" +
"\"id\":1," +
"\"name\":\"唐山\"," +
"\"person.idType\":\"1\"" +
"}" +
"]";
//将json解析为数组
json = eval(json);
//开始赋值
for (int i = 0 ; i < json.length ; i ++) {
// delete json[i]["0"];
list.push(json[i]);
}
//开始ajax请求
//注意⚠️出现 请求出现 “415” 情况是因为未设置 contentType: "application/json; charset=UTF-8",
$.ajax({
type: "post",
contentType: "application/json; charset=UTF-8",
url: "${cxt}/user/person/save",
dataType: "json",
data: JSON.stringify(list),
success: function (data) {
if(data.code == 200){
console.log('success');
} else {
console.log('error');
}
}
});
/*执行上述前端代码后发现又出现400,参数前前后后一番比对终究未发现问题,
然后有点怀疑人生,一顿度娘问候。看各路大神的原因分析:有说类无构造器的,或者无无参构造器的,
映射字段实体没有等等。一同实验均不好使,然后又换了一个类实验、同样的请求,奇迹出现了?好像可以 了; method = RequestMethod.POST加不加无所谓,只有希望强制post请求的时候加
*/
@ResponseBody
@RequestMapping(value = "/person/save")
public Map savePerson(@RequestBody List<User> userList){
Map map = new HashMap();
save(userList);
map.put("code","200");
return map;
}
// 接下来就开始怀疑 是类有问题了,仔细看了下实体;果然有属性进行了set方法重写而且入参还不一样。
接下来set方法上使用了@JsonIgnore 再次重启服务果然OK了。
public void setImparity(Integer imparity) {
this.imparity = imparity;
}
@JsonIgnore
public void setImparity(String imparity) {
this.imparity = Integer.valueOf(imparity);
}