Whatwg Fetch fails when json parsing an empty response, how can I prevent it?

前端 未结 4 1517
小鲜肉
小鲜肉 2021-02-12 20:04

I\'m using the Fetch API both in the frontend and on the backend (NodeJS), a problem that I\'ve been facing a lot happens when parsing the response as json.

respon

4条回答
  •  心在旅途
    2021-02-12 20:18

    Once you have the Response object, inspect the headers and see what Content-Length says. Based on that you can know whether or not there is something to parse. But also, it seems bogus for the server to return an application/json resource that is empty, as that is not JSON.

提交回复
热议问题