我正在使用react native构建一个应用程序,它使得获取调用依赖于来自服务器的最新信息.我注意到它似乎缓存了响应,如果我再次运行该fetch调用,它将返回缓存的响应,而不是来自我的服务器的新信息.
我的功能如下:
goToAll() {
AsyncStorage.getItem('FBId')
.then((value) => {
api.loadCurrentUser(value)
.then((res) => {
api.loadContent(res['RegisteredUser']['id'])
.then((res2) => {
console.log(res2);
this.props.navigator.push({
component: ContentList,
title: 'All',
passProps: {
content: res2,
user: res['RegisteredUser']['id']
}
})
});
});
})
.catch((error) => {console.log(error);})
.done();
}
Run Code Online (Sandbox Code Playgroud)
而api.js im调用的函数如下:
loadContent(userid){
let url = `http://####.com/api/loadContent?User_id=${userid}`;
return fetch(url).then((response) => response.json());
}
Run Code Online (Sandbox Code Playgroud) 我在让我的 fetch POST 调用在 iOS 上工作时遇到了巨大的麻烦。我的标准 Fetch 调用工作正常,而 Fetch POST 调用在 android 上工作正常,但在 iOS 上不工作。
出现的错误是“Possible Unhandled Promise Rejection (id: 0): Unexpected token < in JSON at position 0”
它实际上将发布数据保存到我的服务器,但会引发该错误。
我尝试GLOBAL.XMLHttpRequest = GLOBAL.originalXMLHttpRequest || GLOBAL.XMLHttpRequest;在 API 调用之前调试网络请求,并在我的 chrome 调试工具中使用 CORS。从那里我可以看到它正在一个接一个地拨打两个电话。第一个类型为“OPTIONS”,而第二个类型为“POST”。可能应该注意的是,在使用 CORS 和上面的代码行时,该调用在应用程序中有效。
我很困惑,已经用尽了所有途径。
我用于参考的代码如下。
return fetch(url,{
method: 'post',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
}).then((res) => res.json());
Run Code Online (Sandbox Code Playgroud)