fetch中TypeError: Failed to execute 'json' on 'Response': body stream is locked

2020-03-106963次阅读TypeScript

fetch使用中遇到过这个错误TypeError: Failed to execute 'json' on 'Response': body stream is locked,后来发现它与Response响应状态无关,真正的问题是您只能使用Response.json()一次,如果您多次使用它,就会发生错误。

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()。把上面的两个console.log语句去掉就可以了,我也是打了console.log才出现这种低级错误

上一篇: JS对象转formData对象  下一篇: ERROR Invalid options in vue.config.js: "baseUrl" is not allowed  

fetch中TypeError: Failed to execute 'json' on 'Response': body stream is locked相关文章