JSON中位置0处出现意外的<符号。

3
如果我使用curl命令,服务器将返回一个帖子对象数组,格式如下:
curl http://localhost/api/posts/
[
    {
        "id": 7,
        "target": {
            "body": "This is the body",
            "title": "Airbnb raising a reported $850M at a $30B valuation",
            "user": {
                "first_name": "ben",
                "last_name": "jamin"
            }
        }
    },
    {
        "id": 11,
        "target": {
            "body": "This is the body",
            "title": "Browsing the APISSS",
            "user": {
                "first_name": "user",
                "last_name": "two"
            }
        }
    }
]

我尝试使用fetch API获得这个:

fromServer() {
    console.log('fromServer')
    var headers = new Headers();
    headers.append('Accept', 'application/json');
    let a = fetch('http://test.com/api/posts/', headers)
        .then(function(response) {
                if (response.status !== 200) {
                    console.log('There was a problem. Status Code: ' +  response.status);  
                    return;
                }

                response.json().then(function(data) {  
                    console.log(data);
                });  
            }  
        )  
        .catch(function(err) {  
            console.log('Fetch Error :-S', err);  
        });
}

但是我遇到了这个错误:

JSON 中位置 0 处出现意外的 < 符号

SyntaxError: JSON 中位置 0 处出现意外的 < 符号

我该如何解决这个问题?请你帮帮我。谢谢。


很可能你正在接收一个HTML文档 - 尝试使用 response.text() 而不是 response.json() 来查看你实际上接收到了什么。 - Jaromanda X
我认为应该是这样的 -> ".then(function(response) => { ... } );",你缺少了 "=>"。只是猜测。 - Rajesh
7个回答

2
很明显,在将响应解析为json对象时,您存在一些语法错误。
如果有的话,请从服务器json文件中删除所有注释。
如果不是这样的话: 我认为响应体不是json。
您正在从服务器接收HTML(或XML),但代码无法将其解析为JSON。 在Chrome开发工具的“网络”选项卡中查看服务器响应的内容。
或者使用此代码进行调试:(如“Jaromanda X”所说)
.then(function(response) {
                console.log(response);
                console.log(response.status);
                console.log(response.json());
                console.log(response.text());
                })  
        .catch(function(err) {  
            console.log('Fetch Error :-S', err);  
        });

1
请检查您的服务器返回的数据是否仅为JSON格式,而不是字符串。 对于关联数据,您应该以{开头,而不是[。 因此,您的数据应采用以下格式:
{
  "data": [
    {
      "id": 7,
      "target": {
        "body": "This is the body",
        "title": "Airbnb raising a reported $850M at a $30B valuation",
        "user": {
          "first_name": "ben",
          "last_name": "jamin"
        }
      }
    },
    {
      "id": 11,
      "target": {
        "body": "This is the body",
        "title": "Browsing the APISSS",
        "user": {
          "first_name": "user",
          "last_name": "two"
        }
      }
    }
  ]
}

你可以从response.data中获取所有数据。 如需更多详细信息,请访问此链接

1
你可以尝试添加dataType属性,就像dataType: 'html'dataType: 'text'一样。

0

错误信息的措辞与在Google Chrome中运行JSON.parse('<...')时所得到的相同。我知道您说服务器正在设置Content-Type:application/json,但我认为响应正文实际上是HTML。

"SyntaxError: Unexpected token < in JSON at position 0"

该行下划线标记了console.error(this.props.url, status, err.toString())。err实际上是在jQuery中抛出的,并作为变量err传递给您。该行被标记的原因仅仅是因为您在那里记录它。

我建议您添加日志记录。查看实际的xhr(XMLHttpRequest)属性以了解有关响应的更多信息。尝试添加console.warn(xhr.responseText),您很可能会看到接收到的HTML。

请根据上述说明更正您的代码。


0
所以问题是这样的:由于我是从局域网中的vagrant机器进行测试,并且在我的hosts文件中将vagrant机器的IP地址添加为URL(test.com),因此设备无法获取该URL。在我将vagrant机器更改为端口转发并提供了机器的原始IP地址后,我能够获取JSON对象。感谢大家的帮助。

0
在我的情况下,我尝试使用的端口已经被占用了。我通过在命令提示符中执行以下命令来解决这个问题,以终止该端口。
taskkill /F /IM node.exe

这并没有回答问题。一旦你拥有足够的声望,你就可以评论任何帖子;相反,提供不需要询问者澄清的答案。- 来自审核 - Lajos Arpad

0

所以,你可以使用起始的 JSON 数组;

{

"array": [

    {
        "id": 7,
        "target": {
            "body": "This is the body",
            "title": "Airbnb raising a reported $850M at a $30B valuation",
            "user": {
                "first_name": "ben",
                "last_name": "jamin"
            }
        }
    },
    {
        "id": 11,
        "target": {
            "body": "This is the body",
            "title": "Browsing the APISSS",
            "user": {
                "first_name": "user",
                "last_name": "two"
            }
        }
    }
]

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接