L
L
Leonid Shishkin2019-01-23 22:07:21
API
Leonid Shishkin, 2019-01-23 22:07:21

Can't fetch data from api using fetch?

I have a React app made with create-react-app. In componentDidMount using fetch I make a request to a third-party api, but fetch does not return json.
https://github.com/lshishkin/comic-vine/blob/maste...

async componentDidMount(){
  const heroes=await (await fetch(`https://comicvine.gamespot.com/api/characters/?api_key=6a1a9692ade19104b7ee7188055e048bdd6474a3`,{
    mode:"no-cors"
  })).json()
  console.log(heroes)
}

5c48bd32adcaf139873133.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Somewhere Intech, 2019-01-23
@leonidshishkin

You receive a response opaque (i.e. essentially a reject).
And you will not be able to process the data with .json () because they are xml.
Quote from Habr
As for “opaque”, it comes in cases where a CORS request is made, but the remote resource does not return CORS headers. This type of request does not provide access to the data or the status header, so we cannot judge the result of the request. As part of the current fetch() implementation, it is not possible to execute CORS requests from the window scope, and here is why. This functionality should be added as soon as the Cache API becomes available from the window object.
Source
Alternatively, if you really need to get this data from the client, you can use ServiceWorkers.

A
Andrey Okhotnikov, 2019-01-24
@tsepen

For this request, Fetch returns XML, you need to convert xml to json

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question