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

前端 未结 30 1473
花落未央
花落未央 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:29

    This might be old. But, it just occurred in angular, the content type for request and response were different in my code. So, check headers for ,

     let headers = new Headers({
            'Content-Type': 'application/json',
            **Accept**: 'application/json'
        });
    

    in React axios

    axios({
      method:'get',
      url:'http://  ',
     headers: {
             'Content-Type': 'application/json',
            Accept: 'application/json'
        },
      responseType:'json'
    })
    

    jQuery Ajax:

     $.ajax({
          url: this.props.url,
          dataType: 'json',
    **headers: { 
              'Content-Type': 'application/json',
            Accept: 'application/json'
        },**
          cache: false,
          success: function (data) {
            this.setState({ data: data });
          }.bind(this),
          error: function (xhr, status, err) {
            console.error(this.props.url, status, err.toString());
          }.bind(this)
        });
      },
    
    0 讨论(0)
  • 2020-11-22 05:29

    just something basic to check, make sure you dont have anything commented out in the json file

    //comments here will not be parsed and throw error
    
    0 讨论(0)
  • 2020-11-22 05:30

    The possibilities for this error are overwhelming.

    In my case, I found that the issue was adding the homepage filed in package.json caused the issue.

    Worth checking: in package.json change:

    homepage: "www.example.com"
    

    to

    hompage: ""   
    
    0 讨论(0)
  • 2020-11-22 05:32

    In my case, I was getting this running webpack, and it turned out to be some corruption somewhere in the local node_modules dir.

    rm -rf node_modules
    npm install
    

    ...was enough to get it working right again.

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

    For me, this happened when one of the properties on the object I was returning as JSON threw an exception.

    public Dictionary<string, int> Clients { get; set; }
    public int CRCount
    {
        get
        {
            var count = 0;
            //throws when Clients is null
            foreach (var c in Clients) {
                count += c.Value;
            }
            return count;
        }
    }
    

    Adding a null check, fixed it for me:

    public Dictionary<string, int> Clients { get; set; }
    public int CRCount
    {
        get
        {
            var count = 0;
            if (Clients != null) {
                foreach (var c in Clients) {
                    count += c.Value;
                }
            }
            return count;
        }
    }
    
    0 讨论(0)
  • 2020-11-22 05:33

    This might cause due to your javascript code is looking at some json response and you received something else like text.

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