javascript fetch-无法在“响应"上执行"json":主体流已锁定 [英] javascript fetch - Failed to execute 'json' on 'Response': body stream is locked

查看:65
本文介绍了javascript fetch-无法在“响应"上执行"json":主体流已锁定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当请求状态大于400(我尝试过400、423、429状态)时,提取无法读取返回的json内容.浏览器控制台中显示以下错误

When the request status is greater than 400(I have tried 400, 423, 429 states), fetch cannot read the returned json content. The following error is displayed in the browser console

未捕获(承诺)TypeError:无法在上执行"json" 响应":体内流被锁定

Uncaught (in promise) TypeError: Failed to execute 'json' on 'Response': body stream is locked

我显示了返回的响应对象的内容,如下所示:

I showed the contents of the returned response object as follows:

但是几个月前我仍然可以使用它.

But I can still use it a few months ago.

我的问题如下:

  • 这仅仅是Chrome浏览器的行为还是获取标准更改?
  • 有什么方法可以获取这些状态的身体含量吗?

PS:我的浏览器版本是Google Chrome 70.0.3538.102(正式版本)(64位)

PS: My browser version is Google Chrome 70.0.3538.102(正式版本) (64 位)

推荐答案

我也遇到了此错误,但发现它与Response的状态无关,真正的问题是您只能消耗一次Response.json(),如果如果您不止一次使用它,则会发生错误.

I met this error too but found out it is not related to the state of Response, the real problem is that you only can consume Response.json() once, if you are consuming it more than once, the error will happen.

如下所示:

    fetch('http://localhost:3000/movies').then(response =>{
    console.log(response);
    if(response.ok){
         console.log(response.json()); //first consume it in console.log
        return response.json(); //then consume it again, the error happens

    }

因此解决方案是避免在then块中多次消耗Response.json().

So the solution is to avoid consuming Response.json() more than once in then block.

这篇关于javascript fetch-无法在“响应"上执行"json":主体流已锁定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆