Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
22
rated 0 times [  29] [ 7]  / answers: 1 / hits: 149497  / 6 Years ago, wed, november 28, 2018, 12:00:00

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




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:



enter



But I can still use it a few months ago.



My question is as follows:




  • Is this just the behavior of the Chrome browser or the fetch standard changes?

  • Is there any way to get the body content of these states?



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


More From » fetch-api

 Answers
4

According to MDN, you should use Response.clone():



The clone() method of the Response interface creates a clone of a response object, identical in every way, but stored in a different variable. The main reason clone() exists is to allow multiple uses of Body objects (when they are one-use only.)



Example:


fetch('yourfile.json').then(res=>res.clone().json())

[#53028] Friday, November 23, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
sonja

Total Points: 541
Total Questions: 113
Total Answers: 114

Location: Anguilla
Member since Sun, Jan 29, 2023
1 Year ago
sonja questions
Mon, Nov 30, 20, 00:00, 4 Years ago
Sun, Oct 11, 20, 00:00, 4 Years ago
Thu, May 21, 20, 00:00, 4 Years ago
Sun, Nov 10, 19, 00:00, 5 Years ago
Mon, Aug 26, 19, 00:00, 5 Years ago
;