javascript fetch - Failed to execute 'json' on 'Response': body stream is locked

前端 未结 8 1731
滥情空心
滥情空心 2021-01-30 09:58

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 cons

8条回答
  •  闹比i
    闹比i (楼主)
    2021-01-30 10:37

    Response methode like 'json', 'text' can be called once, and then it locks. The posted image of response shows that body is locked. This means you have already called the 'then', 'catch'. To reslove this you can try the following.

    fetch(url)
        .then(response=> response.body.json())
        .then(myJson=> console.log(myJson))
    

    Or

    fetch(url)
        .catch(response=> response.body.json())
        .catch(myJson=> console.log(myJson))
    

提交回复
热议问题