猿问

JSON和解析问题

后台返回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"
Cats萌萌
浏览 536回答 4
4回答

慕田峪7331174

先确认一下 content-type 吧,response 的 content-type 是 json 么?看报错不太像,很像是返回了 text/html 的 header

holdtom

看起来像是中文问题。前后台编码一致吗?

慕容708150

返回来的是json字符串,需要处理一下 success: function(data) { var data = JSON.parse(data) if (data.code == '2000') { userType = data.result.userType; } }
随时随地看视频慕课网APP

相关分类

Java
我要回答