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

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

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

    public Dictionary 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 Clients { get; set; }
    public int CRCount
    {
        get
        {
            var count = 0;
            if (Clients != null) {
                foreach (var c in Clients) {
                    count += c.Value;
                }
            }
            return count;
        }
    }
    

提交回复
热议问题