react-native fetch返回奇数响应
问题描述:
我正在使用fetch从这样的API中获取一些东西:
I'm using fetch to get some stuff from an API like this:
fetch('http://facebook.github.io/react-native/movies.json')
.then(
data => console.log(data),
error => console.log(error)
)
但我得到的是以下对象,而不是实际数据
But what I get back is the following object, not the actual data
_bodyBlob: Blob
_bodyInit: Blob
headers: Headers
ok: true
status: 200
statusText: undefined
type: "default"
url: "http://facebook.github.io/react-native/movies.json"
有人能解释我的错吗?
我做错了什么?
Can someone explain me whats wrong? I'm doing something wrong?
答
要从响应中获取数据,您必须调用 data.json();
示例:
To get the data from the response you have to call data.json();
Example:
fetch('http://facebook.github.io/react-native/movies.json')
.then((response) => response.json())
.then((data) => {
console.log(data);
})
.catch((error) => {
console.log(error);
});
(假设响应在json中)。
(Assuming the response is in json).