简体   繁体   中英

React-Native JSON fetch from URL

I hope I could find some help to the issue.

I'm using React Native and try to get some data from an API called Feiertage-API ( https://feiertage-api.de/ ), that basically returns (should return) the official holidays in Germany.

Trying to use fetch in react native, returns:

Response {
   "_bodyBlob": Blob {
     "_data": Object {
       "blobId": "49C4AD4B-4648-44DB-AED7-7654EB78EF7A",
       "name": "api",
       "offset": 0,
       "size": 487,
       "type": "application/json",
     },
   },
   "_bodyInit": Blob {
     "_data": Object {
       "blobId": "49C4AD4B-4648-44DB-AED7-7654EB78EF7A",
       "name": "api",
       "offset": 0,
       "size": 487,
       "type": "application/json",
     },
   },
   "headers": Headers {
     "map": Object {
       "access-control-allow-origin": Array [
         "*",
       ],
       "content-type": Array [
         "application/json",
       ],
       "date": Array [
         "Tue, 31 Jul 2018 07:17:51 GMT",
       ],
       "server": Array [
         "nginx",
       ],
       "x-powered-by": Array [
         "PHP/7.0.31, PleskLin, PleskLin",
       ],
     },
   },
   "ok": true,
   "status": 200,
   "statusText": undefined,
   "type": "default",
   "url": "https://feiertage-api.de/api/?jahr=2019&nur_land=hb",
}

My fetch call looks as follows:

fetch('https://feiertage-api.de/api/?jahr=2019&nur_land=hb')
                .then(response => console.log(response) )
                .catch(error => console.log(error));

GET-Parameters are: - jahr=2019 (for year, I think that's obvious) - nur_land=hb (specifying what state by short form)

The possibility is given to get JSONP by adding the get-parameter 'callback=mycallbackname'

If trying to show the data with: console.log(response.json())

the result is:

 Promise {
      "_40": 0,
      "_55": null,
      "_65": 0,
      "_72": null,
   }

What I'm expecting to see is exactly the data which is shown here -> https://feiertage-api.de/api/?jahr=2019&nur_land=hb

Would like to know how to extract the data out of the Response{....}.

You need to call .json() method on that response.

fetch('https://feiertage-api.de/api/?jahr=2019&nur_land=hb')
            .then(response => response.json() )
            .then(data => console.log(data) )
            .catch(error => console.log(error));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM