Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fetch API - using response.json() throws an unexpected end of input error [duplicate]

When trying to resolve a fetch promise with JS is set the mode to 'no-cors' based on this answer. However setting the mode to 'cors' results in having:

Access to fetch at '{endpoint}' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

So I wrote this in my function:

search() {
    return fetch(`${baselink}${summonerEndpoint}${summoner}${apikey}`, {mode: 'no-cors'}).then(response => {
      return response.json()
    }).then(jsonResponse => {
      console.log(jsonResponse);
      if (!jsonResponse.info) {
        return [];
      }
      return jsonResponse.info.items.map(info => ({
        id: info.id,
        accountId: info.accountId,
        name: info.name,
        profileIconId: info.profileIconId,
        revisionDate: info.revisionDate,
        summonerLevel: info.summonerLevel
      }));
    });
  }

This results in following error Uncaught (in promise) SyntaxError: Unexpected end of input for return response.json(), but with no further message. What am I doing wrong?

like image 396
Peter Avatar asked Nov 23 '18 07:11

Peter


People also ask

What is response JSON () in fetch?

Response.json()The json() method of the Response interface takes a Response stream and reads it to completion. It returns a promise which resolves with the result of parsing the body text as JSON .

How do I fix fetch error in CORS?

To solve the "TypeError: Failed to fetch", make sure to pass the correct configuration to the fetch method, including the URL, HTTP method and headers, and verify that the server you're making a request to is setting the correct CORS headers with the response. Copied!

How do I debug unexpected end of JSON input?

You can solve the "Unexpected end of JSON input" error in the following 3 ways: wrap your parsing logic in a try/catch block. make sure to return a valid JSON response from your server. remove the parsing logic from your code if you are expecting an empty server response.

What is response in Fetch?

The Response interface of the Fetch API represents the response to a request. You can create a new Response object using the Response() constructor, but you are more likely to encounter a Response object being returned as the result of another API operation—for example, a service worker FetchEvent.


1 Answers

If an opaque response serves your needs

It doesn't. You want to see the response. You can't see an opaque response (that is what opaque response means).

no-cors mode means that if the browser has to do anything that requires permission from CORS, it will fail silently instead of throwing an error.

So it is silently failing to get the response, then trying to parse that nothing as JSON (which throws a different error).

You need:

  • To not use no-cors mode
  • The server to grant permission using CORS

See this question for more information about CORS in general.

like image 50
Quentin Avatar answered Oct 20 '22 00:10

Quentin