【发布时间】:2017-11-22 15:01:22
【问题描述】:
我在 React Native 中使用 fetch 来调用我的 API,但是,它只有 75% 的时间有效。 当我的请求不起作用时,我会得到这个:
TypeError:网络请求失败
或
SyntaxError: Unexpected token
fetch('http://localhost/vision.php', {
method: 'POST',
headers: {
'Accept': 'application.json',
'Content-Type': 'application.json',
},
body: JSON.stringify({
key: 'Mon paramètre'
})
})
.then((data) => data.json())
.then((dataJson) => {
console.log(dataJson.message);
})
.catch((error) => {
console.log(error);
});
}
谁能解释一下?
【问题讨论】:
-
如果你在这一行后面加上“if (dataJson !== null) {...”:“.then((dataJson) => {”,它仍然会发生吗?
标签: api react-native fetch fetch-api