简体   繁体   English

React fetch为我提供了截断的JSON响应

[英]React fetch gives me a truncated JSON response

I'm facing an issue in React fetch, it gives me an incomplete response, so when I parse the response into JSON, it was returning me an error. 我在React fetch中遇到了一个问题,它给了我一个不完整的响应,所以当我将响应解析为JSON时,它给我一个错误。

Code: 码:

export default async (url, body = null, method = 'GET') => {
  let config = {
      method,
  };
  try {
      const response = await fetch(url, config);
      if (!response.ok) {
          throw new Error(response.statusText);
      }
      return await response.json();
  } catch (error) {
      console.warn(error);
      throw error;
  }
};

Response log: 响应日志: 响应日志

Does fetch has a maximum response size? fetch是否具有最大响应大小? If yes, how to increase it? 如果是,如何增加它?

There's no max limit to fetch response size. 获取响应大小没有最大限制。

The error in your case means that you are reading the response body more than once. 您的案例中的错误意味着您正在多次阅读响应正文。 Try guarding the .json() calls with Body.bodyUsed . 尝试使用Body.bodyUsed .json()调用。

See https://developer.mozilla.org/en-US/docs/Web/API/Body 请参阅https://developer.mozilla.org/en-US/docs/Web/API/Body

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM