fetch() unexpected end of input

前端 未结 5 2264
暖寄归人
暖寄归人 2020-11-30 02:34

I am using fetch() to grab data from api server. My error looks like this:

Uncaught (in promise) SyntaxError: Unexpected end of input at 
  fetch.then.blob.
         


        
5条回答
  •  孤城傲影
    2020-11-30 02:55

    Opaque Responses

    A response for a no-cors request to a cross-origin resource has a response type of 'opaque'. If you log the response before trying to turn it to JSON, you will see a type of "opaque".

    Opaque types are listed as "severely restricted" as explained in the fetch spech on whatwg.org.

    An opaque filtered response is a filtered response whose type is "opaque", url list is the empty list, status is 0, status message is the empty byte sequence, header list is empty, body is null, and trailer is empty.

    They cannot currently be read when the type is opaque as explained on Google's docs on the opaque type.

    An opaque response is for a request made for a resource on a different origin that doesn't return CORS headers. With an opaque response, we won't be able to read the data returned or view the status of the request, meaning we can't check if the request was successful or not. With the current fetch() implementation, it's not possible to make requests for resources on a different origin from the window global scope.

    Enable CORS support

    This can be environment dependant or language dependant. For example, you can change CORS settings within Nginx's environment by changing your server config, or you can specify headers within your application code such as in PHP.

    I highly recommend reading the Mozilla documentation on CORS requests.

    An example in PHP:

提交回复
热议问题