【问题标题】:Fetch fails to receieve large responsesFetch 无法收到大量响应
【发布时间】:2021-09-10 20:51:41
【问题描述】:

我正在尝试在 react-native(js) 中获取,但是响应被破坏了很多次,尤其是对于更大的数据!

响应是一个数组,对于数组大小 20 提取几乎永远不会有效,给出以下错误:

[未处理的承诺拒绝:SyntaxError: JSON Parse error: Expected ']']

这是用于获取的代码:

        fetch(url)
        .then(response => {
            if(response.status!==200){alert('Something went wrong, please try again later');return null}
            return response.json()
        })
        .then(response => {
            if (response == null){return}
            console.log(response)
        })

错误发生在'return response.json()'行。

请帮忙!谢谢。

【问题讨论】:

    标签: json reactjs fetch response parse-error


    【解决方案1】:

    您可能需要检查从服务器发回的 json。错误发生在格式错误的 json 上。但是,这也可能是您格式化获取的方式。

    这是一些从 api 获取 json 的代码。这是 matt gaunt 的文章中的一个很好的例子,https://developers.google.com/web/updates/2015/03/introduction-to-fetch#:~:text=The%20response%20of%20a%20fetch,the%20stream%20will%20happen%20asynchronously

    fetch('./api/some.json')
      .then(
        function(response) {
          if (response.status !== 200) {
            console.log('Looks like there was a problem. Status Code: ' +
              response.status);
            return;
          }
    
          // Examine the text in the response
          response.json().then(function(data) {
            console.log(data);
          });
        }
      )
      .catch(function(err) {
        console.log('Fetch Error :-S', err);
      });
    

    【讨论】:

      猜你喜欢
      • 2020-03-26
      • 1970-01-01
      • 2019-10-03
      • 1970-01-01
      • 1970-01-01
      • 2018-01-25
      • 1970-01-01
      • 2021-06-02
      • 2022-01-03
      相关资源
      最近更新 更多