JSON and parsing problems

the logic for the background to return the json result is

    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();
 

get it at the front desk:

{"code":"2000","message":"","result":{"phone":"15365166305","channel":"null","userType":""}}

but what if there is a parsing error in 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 says yes:

Unexpected token < in JSON at position 0"
Feb.28,2021

in fact, what the foreground gets is definitely not the JSON string you posted. You need to find the response of the specified request under the network bar of the browser to confirm that it is not the end of seeing this in a page.

possible results are similar:

<html>
<body>
{"code":"2000","message":"","result":{"phone":"15365166305","channel":"null","userType":""}}
</body>
</html>

Let's confirm content-type first. Is the content-type of response json? Reading a newspaper error is not very similar. It looks like a header


returned text/html looks like a Chinese problem. Are the front and background codes the same?


the json string is returned, which needs to be processed

 success: function(data) {
      var data = JSON.parse(data)
      if (data.code == '2000') {
        userType = data.result.userType;
      }
    }
Menu