JQuery ajax post json 出现 JSON parse error: Unrecognized token 错误
本文内容已经过时或失效,仅当作存档使用
现象
ajax 代码:
$.ajax({
type:"POST",
url:"/air-conditioner",
data:{
"setTemperature":temp,
"sensitivity":sensitivity,
"highTemperatureLimit":temp_limit
},
contentType:"application/json; charset=utf-8",
dataType:"json"
});SpringMVC 报错:
org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Unrecognized token 'setTemperature': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false');原因
后端如果用 SpringMVC 的@RequestBody注解的话,则只能接收 json 对象的字符串,不能接收 json对象,用 JSON.stringify(data)的方式将对象变成字符串,同时 ajax 请求也要指定dataType: “json”,contentType:“application/json” ,这样就能用@RequestBody注解绑定对象或者 bList 集合.
本文参考:
jquery发送ajax请求出现JSON parse error: Unrecognized token 'id': was expecting ('true', 'false' or 'null)
评论已关闭