后台返回json结果的逻辑是
Map<String, Object> resultMap = new HashMap<>();
resultMap.put("userType", userType);
resultMap.put("phone", phone);
resultMap.put("channel", channel);
JSONObject obj = new JSONObject();
obj.put("code", "2000");
obj.put("message", "成功");
obj.put("result", resultMap);
return obj.toJSONString();
前台得到:
{"code":"2000","message":"成功","result":{"phone":"15365166305","channel":"null","userType":"高级版用户"}}
可是在ajax里出现解析错误,怎么办?
success: function(data){
if(data.code == '2000'){
userType = data.result.userType;
}
},
error: function(request, textStatus, errorThrown){
console.log(request.status);
console.log(request.readyState);
}
errorThrown说是:
Unexpected token < in JSON at position 0"
实际上前台得到的肯定不是你贴的那一串 JSON 字符串, 你需要在浏览器network 栏下面找到指定请求的响应来确认,并不是看到一个 页面里面是这个就完了。
有可能的结果类似: