React Native 获取不同的结果 with/without wifi
React Native fetch different result with/without wifi
我在我的设备上测试我的 ReactNative-App 时遇到了一些奇怪的行为。你在哪里可以告诉我我做错了什么。
首先我有一个 REST-API 返回帖子列表。这里有一些 postman 请求的输出(与稍后在应用程序中相同 url):
{
"status": 200,
"data": [
{
"id": "6bb373c6dbe8c4782121068223a1d5bf710a14a62943443bdc5c017ce873935a",
"description": "",
"upvotes": 0,
"downvotes": 0,
"creationTime": "2017-08-16T04:44:03Z"
},
{
"id": "5ae791aa33512dc179f075649f68731f269c366a37807f7da9eeeabafcda8c7d",
"description": "",
"upvotes": 0,
"downvotes": 0,
"creationTime": "2017-08-16T04:42:27Z"
},
],
"version": "0.0.1",
"servertime": "2017-08-16T07:02:32.250978864+02:00"
}
我希望通过 wifi 和 移动互联网获得此对象。
这里是在我的应用程序中请求我的 API 的代码片段:
exports.getPosts = function() {
return fetch(HTTP.baseurl + "/image", {
method: "GET"
}).then(res => res.json()).then((res)=>{
//this is the line you will see the output from
console.log(JSON.stringify(res, null, 2));
return res
});
}
如果使用 wifi 请求 API,我会得到与邮递员中相同的对象。
但如果通过移动互联网请求,我会得到以下对象:
{
"status": 200,
"data": {
"id": "5ae791aa33512dc179f075649f68731f269c366a37807f7da9eeeabafcda8c7d",
"description": "",
"upvotes": 0,
"downvotes": 0,
"creationTime": "2017-08-16T06:42:27.37228137+02:00"
},
"version": "0.0.1",
"servertime": "2017-08-16T06:42:27.4874009+02:00"
}
测试于:
macOS: 10.12.6
XCode: 8.3.3
iOS: 10.3.3 (iPhone 6)
node: 8.4.0
react: 16.0.0-alpha.12
react-native: 0.45.1
编辑:
我还在一个新的 React Native 应用程序上复制了它,只插入了以下代码行:
//first send an image to the API
fetch("http://services.impressions-app.com/test/image", {
method: "POST",
body: JSON.stringify({"blob": blob, "description": ""})
}).then(res => res.json()).then(res => {
//then request all the images including the new one
return fetch("http://services.impressions-app.com/test/image", {
method: "GET"
})
}).then(res => res.json()).then(res => {
//should log an array of imageobjects (see postman response above)
console.log("LOADED", res);
}).catch(e => {
console.error(e)
});
我得到了一个像上面这样的对象,其中数据作为对象而不是数组:
{
"status": 200,
"data": {
"id": "06c50436c2b2b671a64ce3b57b6cac76b648a83ed621305a0f6a55f8186957ba",
"description": "",
"upvotes": 0,
"downvotes": 0,
"creationTime": "2017-08-17T08:00:24.198626835+02:00"
},
"version": "0.0.1",
"servertime": "2017-08-17T08:00:26.031231455+02:00"
}
测试于:
macOS: 10.12.6
XCode: 8.3.3
iOS: 10.3.3 (iPhone 6)
node: 8.4.0
react: 16.0.0-alpha.12
react-native: 0.45.1
编辑#2:
我查看了日志文件,它仅在我通过 wifi 请求时才记录此请求。其他请求已按预期记录。
可能是缓存问题?
大家有遇到过类似的问题吗?
非常感谢
问题如下:
短版:
这是一个缓存问题。
如果你想确定再次调用API,你可以给url添加一个自动递增。
"https://yourapi.com?1"
"https://yourapi.com?2"
"https://yourapi.com?3"
长版:
在触发上述请求之前,我触发了另一个请求,即 POST
到相同的 URL。 post 返回了一个这样的对象:
{
"status": 200,
"data": {
"id": "06c50436c2b2b671a64ce3b57b6cac76b648a83ed621305a0f6a55f8186957ba",
"description": "",
"upvotes": 0,
"downvotes": 0,
"creationTime": "2017-08-17T08:00:24.198626835+02:00"
},
"version": "0.0.1",
"servertime": "2017-08-17T08:00:26.031231455+02:00"
}
当通过 GET
请求原始 URL 时缓存并返回此对象。
这对于 React Native 来说可能是个问题,因为我预计只能从我之前使用相同方法调用的 URLs 中获取 URLs 的缓存值。
像这样:
POST "https://yourapi.com" => "foo"
GET "https://yourapi.com" => "bar" (not cached)
GET "https://yourapi.com" => "bar" (cached)
并且不是这样的:
POST "https://yourapi.com" => "foo"
GET "https://yourapi.com" => "foo" (cached)
GET "https://yourapi.com" => "foo" (cached)
我在我的设备上测试我的 ReactNative-App 时遇到了一些奇怪的行为。你在哪里可以告诉我我做错了什么。
首先我有一个 REST-API 返回帖子列表。这里有一些 postman 请求的输出(与稍后在应用程序中相同 url):
{
"status": 200,
"data": [
{
"id": "6bb373c6dbe8c4782121068223a1d5bf710a14a62943443bdc5c017ce873935a",
"description": "",
"upvotes": 0,
"downvotes": 0,
"creationTime": "2017-08-16T04:44:03Z"
},
{
"id": "5ae791aa33512dc179f075649f68731f269c366a37807f7da9eeeabafcda8c7d",
"description": "",
"upvotes": 0,
"downvotes": 0,
"creationTime": "2017-08-16T04:42:27Z"
},
],
"version": "0.0.1",
"servertime": "2017-08-16T07:02:32.250978864+02:00"
}
我希望通过 wifi 和 移动互联网获得此对象。
这里是在我的应用程序中请求我的 API 的代码片段:
exports.getPosts = function() {
return fetch(HTTP.baseurl + "/image", {
method: "GET"
}).then(res => res.json()).then((res)=>{
//this is the line you will see the output from
console.log(JSON.stringify(res, null, 2));
return res
});
}
如果使用 wifi 请求 API,我会得到与邮递员中相同的对象。 但如果通过移动互联网请求,我会得到以下对象:
{
"status": 200,
"data": {
"id": "5ae791aa33512dc179f075649f68731f269c366a37807f7da9eeeabafcda8c7d",
"description": "",
"upvotes": 0,
"downvotes": 0,
"creationTime": "2017-08-16T06:42:27.37228137+02:00"
},
"version": "0.0.1",
"servertime": "2017-08-16T06:42:27.4874009+02:00"
}
测试于:
macOS: 10.12.6
XCode: 8.3.3
iOS: 10.3.3 (iPhone 6)
node: 8.4.0
react: 16.0.0-alpha.12
react-native: 0.45.1
编辑:
我还在一个新的 React Native 应用程序上复制了它,只插入了以下代码行:
//first send an image to the API
fetch("http://services.impressions-app.com/test/image", {
method: "POST",
body: JSON.stringify({"blob": blob, "description": ""})
}).then(res => res.json()).then(res => {
//then request all the images including the new one
return fetch("http://services.impressions-app.com/test/image", {
method: "GET"
})
}).then(res => res.json()).then(res => {
//should log an array of imageobjects (see postman response above)
console.log("LOADED", res);
}).catch(e => {
console.error(e)
});
我得到了一个像上面这样的对象,其中数据作为对象而不是数组:
{
"status": 200,
"data": {
"id": "06c50436c2b2b671a64ce3b57b6cac76b648a83ed621305a0f6a55f8186957ba",
"description": "",
"upvotes": 0,
"downvotes": 0,
"creationTime": "2017-08-17T08:00:24.198626835+02:00"
},
"version": "0.0.1",
"servertime": "2017-08-17T08:00:26.031231455+02:00"
}
测试于:
macOS: 10.12.6
XCode: 8.3.3
iOS: 10.3.3 (iPhone 6)
node: 8.4.0
react: 16.0.0-alpha.12
react-native: 0.45.1
编辑#2:
我查看了日志文件,它仅在我通过 wifi 请求时才记录此请求。其他请求已按预期记录。
可能是缓存问题?
大家有遇到过类似的问题吗?
非常感谢
问题如下:
短版:
这是一个缓存问题。
如果你想确定再次调用API,你可以给url添加一个自动递增。
"https://yourapi.com?1"
"https://yourapi.com?2"
"https://yourapi.com?3"
长版:
在触发上述请求之前,我触发了另一个请求,即 POST
到相同的 URL。 post 返回了一个这样的对象:
{
"status": 200,
"data": {
"id": "06c50436c2b2b671a64ce3b57b6cac76b648a83ed621305a0f6a55f8186957ba",
"description": "",
"upvotes": 0,
"downvotes": 0,
"creationTime": "2017-08-17T08:00:24.198626835+02:00"
},
"version": "0.0.1",
"servertime": "2017-08-17T08:00:26.031231455+02:00"
}
当通过 GET
请求原始 URL 时缓存并返回此对象。
这对于 React Native 来说可能是个问题,因为我预计只能从我之前使用相同方法调用的 URLs 中获取 URLs 的缓存值。
像这样:
POST "https://yourapi.com" => "foo"
GET "https://yourapi.com" => "bar" (not cached)
GET "https://yourapi.com" => "bar" (cached)
并且不是这样的:
POST "https://yourapi.com" => "foo"
GET "https://yourapi.com" => "foo" (cached)
GET "https://yourapi.com" => "foo" (cached)