“SyntaxError: Unexpected token < in JSON at position 0”

前端 未结 30 1469
花落未央
花落未央 2020-11-22 04:39

In a React app component which handles Facebook-like content feeds, I am running into an error:

Feed.js:94 undefined \"parsererror\" \"SyntaxError: Un

相关标签:
30条回答
  • 2020-11-22 05:15

    The wording of the error message corresponds to what you get from Google Chrome when you run JSON.parse('<...'). I know you said the server is setting Content-Type:application/json, but I am led to believe the response body is actually HTML.

    Feed.js:94 undefined "parsererror" "SyntaxError: Unexpected token < in JSON at position 0"

    with the line console.error(this.props.url, status, err.toString()) underlined.

    The err was actually thrown within jQuery, and passed to you as a variable err. The reason that line is underlined is simply because that is where you are logging it.

    I would suggest that you add to your logging. Looking at the actual xhr (XMLHttpRequest) properties to learn more about the response. Try adding console.warn(xhr.responseText) and you will most likely see the HTML that is being received.

    0 讨论(0)
  • 2020-11-22 05:15

    I my case the error was a result of me not assigning my return value to a variable. The following caused the error message:

    return new JavaScriptSerializer().Serialize("hello");
    

    I changed it to:

    string H = "hello";
    return new JavaScriptSerializer().Serialize(H);
    

    Without the variable JSON is unable to properly format the data.

    0 讨论(0)
  • 2020-11-22 05:17

    In python you can use json.Dump(str) before send result to html template. with this command string convert to correct json format and send to html template. After send this result to JSON.parse(result) , this is correct response and you can use this.

    0 讨论(0)
  • 2020-11-22 05:18

    You're receiving HTML (or XML) back from the server, but the dataType: json is telling jQuery to parse as JSON. Check the "Network" tab in Chrome dev tools to see contents of the server's response.

    0 讨论(0)
  • 2020-11-22 05:19

    SyntaxError: Unexpected token < in JSON at position 0


    You are getting an html file instead of json.

    Html files begin with <!DOCTYPE html>.

    I "achieved" this error by forgetting the https:// in my fetch method:

    fetch(`/api.github.com/users/${login}`)
        .then(response => response.json())
        .then(setData);
    

    I verified my hunch:

    I logged the response as text instead of JSON.

    fetch(`/api.github.com/users/${login}`)
        .then(response => response.text())
        .then(text => console.log(text))
        .then(setData);
    

    Yep, an html file.

    Solution:

    I fixed the error by adding back the https:// in my fetch method.

    fetch(`https://api.github.com/users/${login}`)
        .then(response => response.json())
        .then(setData)
        .catch(error => (console.log(error)));
    
    0 讨论(0)
  • 2020-11-22 05:20

    For some, this may help you guys: I had a similar experience with Wordpress REST API. I even used Postman to check if I had the correct routes or endpoint. I later found out that I accidentally put an "echo" inside my script - hooks:

    Debug & check your console

    Cause of the error

    So basically, this means that I printed a value that isn't JSON that is mixed with the script that causes AJAX error - "SyntaxError: Unexpected token r in JSON at position 0"

    0 讨论(0)
提交回复
热议问题