springboot 接收参数,解析json出现错误:was expecting 'null', 'true', 'false' or NaN
错误信息converter.HttpMessageNotReadableException: JSON parse error: Unrecognized token 'token': was expecting 'null', 'true', 'false' or NaN; nested exception is com.fasterxml.jackson.core.JsonParseExce.
·
错误信息
converter.HttpMessageNotReadableException: JSON parse error: Unrecognized token 'token': was expecting 'null', 'true', 'false' or NaN; nested exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'token': was expecting 'null', 'true', 'false' or NaN
后端接收方法-参数接受
@PostMapping("/doLogin")
public String login(@RequestBody Map map, HttpSession session){
try {
String account = map.get("account")==null?"":map.get("account").toString();
String password = map.get("password")==null?"":map.get("password").toString();
请求方法
<script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script>
$(function(){
$.ajax({
// url:"http://172.17.172.45:8089/account/admin/consumeRecord/getlist",
url:"http://localhost:8089/account/admin/consumeRecord/getlist",
//url:"http://172.17.172.45:8089/account/doLogin",
data:{account: "pan", password: "123456"},
dataType: "json", // 表示返回类型
type:"POST", // 请求方式
crossDomain:true, //是否跨域 可后端设置
contentType:"application/json;charset=UTF-8",
//contentType: "application/x-www-form-urlencoded",
success:function(data){
console.log(data);
}
});
});
</script>
错误信息是400,如下所示:
参数是用等号连接的。
加入JSON.stringify()对传参进行解析;代码如下。
<script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script>
$(function(){
$.ajax({
// url:"http://172.17.172.45:8089/account/admin/consumeRecord/getlist",
// url:"http://172.17.172.45:8089/account/admin/consumeRecord/getlist", ok
url:"http://localhost:8089/account/doLogin",
data:JSON.stringify({account: "pan", password: "123456"}),
dataType: "json", // 表示返回类型
type:"POST", // 请求方式
crossDomain:true, //是否跨域 可后端设置
contentType:"application/json;charset=UTF-8",
//contentType: "application/x-www-form-urlencoded",
success:function(data){
console.log(data);
}
});
});
</script>
后端可以正常接收了。
更多推荐
所有评论(0)