我希望你能帮我解决问题。我正在使用React Native,试图从一个名为Feiertage-API(https://feiertage-api.de/)的API获取一些数据,它基本上返回(应该返回)德国的官方假期。尝试在React Native中使用fetch时,会返回:
我希望看到的正是这里所显示的数据 -> https://feiertage-api.de/api/?jahr=2019&nur_land=hb 想知道如何从Response{...}中提取数据。
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",
}
我的fetch调用如下:
fetch('https://feiertage-api.de/api/?jahr=2019&nur_land=hb')
.then(response => console.log(response) )
.catch(error => console.log(error));
GET参数是: - jahr=2019(表示年份,我认为这很明显) - nur_land=hb(使用简短的形式指定州)
可以通过添加GET参数“callback=mycallbackname”来获取JSONP
如果尝试使用以下代码显示数据: console.log(response.json())
结果是:
Promise {
"_40": 0,
"_55": null,
"_65": 0,
"_72": null,
}
我希望看到的正是这里所显示的数据 -> https://feiertage-api.de/api/?jahr=2019&nur_land=hb 想知道如何从Response{...}中提取数据。
response.json()
返回一个 Promise,因此您必须要么使用await
等待它完成,要么将其包装在.then()
中。 - Kaarel Allemann