[Switch] Common error reporting solutions in JS debugging (Uncaught SyntaxError: Unexpected token o in JSON at position 1)

Error:

Uncaught SyntaxError: Unexpected token o in JSON at position 1

at JSON.parse (<anonymous>)
at Function.m.parseJSON (jquery.js:8515)
at Object.success (crud.html:45)
at j (jquery.js:3143)
at Object.fireWith [as resolveWith] (jquery.js:3255)
at x (jquery.js:9309)
at XMLHttpRequest.b (jquery.js:9713)
(translation: uncaptured syntax error: unrecognized tag < found at json data position 0)

Reason: json format error

return data statement use echo json_encode($data,true); but use echo json_decode($data);
because The function is not used correctly, the returned data is not json data, the first character is "<", so an error Unexpected token < in JSON at position 0 is reported (translation: unrecognized token < found at position 0 of json data); 
if you use $. ajax({}) instead of $.get() Difference: When using $.ajax, the JSON string returned by php has been used by the attribute dataType in ajax. The requested parameter type is required to be String type (dataType:'json') It is returned as a JSON object and does not need to be converted again; when using $.get, the data format returned by the server is the original string array. Therefore, it is necessary to convert the returned string into an array, and use JSON.parse(jsonString) to convert JSON string is parsed into JSON object;
see json tutorial: http://www.w3school.com.cn/json/index.asp


Solution:

Modify it to echo json_encode($data,true);
remove the php code debugging code echo(), and such an error will no longer be reported:

$json = json_encode(array(
      "resultCode"=>200,
      "message"= >"The query is successful!",
      "data"=>$data
    ),JSON_UNESCAPED_UNICODE);
    //Comment out
    /*echo($json);*/
If you use $.ajax, no need to comment
 //No need to comment
echo( $json);

If this is not the problem, please check whether the data returned by the php file is in standard json format. If there are var_dump, echo, comments, etc. in the php file, it will affect the returned json data.


you may be interested in

  1. Detailed explanation of 10 major JavaScript errors from 1000+ project data analysis
  2. Prompt "username or password is incorrect" is bad
  3. Debug front-end HTML/CSS

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325603356&siteId=291194637