use*_*367 6 ajax android android-emulator react-native fetch-api
我正在使用React Native开发Android应用程序.我正在使用在Mac上运行的Android Studio中模拟的Galaxy Nexus API 23上测试我的应用.
我希望允许用户彼此共享信息.我正在运行一个带有php和mysql的web服务器,我希望应用程序向服务器发出请求,以便在用户之间共享数据.
我正在尝试使用Fetch API向我的开发服务器发出POST请求.我的js代码如下:
var AjaxEngine = {
test: function() {
return fetch('http://10.0.2.2/test.json')
.then((response) => response.json())
.then((responseJson) => {
console.log(responseJson);
return responseJson.movies;
})
.catch((error) => {
console.error(error);
});
}
};
Run Code Online (Sandbox Code Playgroud)
叫做 AjaxEngine.test();
在我的模拟器中,我收到以下错误:
ExceptionsManager.js:70 TypeError:XMLHttpRequest.xhr.onerror(http:// localhost:8081/index.android.bundle?platform = android&dev = true&hot = false&minify = false:23711:8)的XMLHttpRequest.dispatchEvent 网络请求失败(http:// localhost:8081/index.android.bundle?platform = android&dev = true&hot = false&minify = false:9740:15)XMLHttpRequest.setReadyState(http:// localhost:8081/index.android.bundle?platform = android&dev =真热=假缩小=假:25157:6)在XMLHttpRequest的.__ didCompleteResponse(HTTP://本地主机:8081/index.android.bundle平台=机器人&dev的=真热=假缩小=假:25015:6)在HTTP://本地主机:8081/index.android.bundle?platform = android&dev = true&hot = false&minify = false:25090:52 在RCTDeviceEventEmitter.emit(http:// localhost:8081/index.android.bundle?platform = android&dev = true&hot = false&minify = false :8977:23)在的MessageQueue .__ callFunction(HTTP://本地主机:8081/index.android.bundle平台=机器人&dev的=真热=假缩小=假:7065:23)在HTTP://本地主机 :8081/index.android.bundle?platform = android&dev = true&hot = false&minify = false:6969:8 at guard(http:// localhost:8081/index.android.bundle?platform = android&dev = true&hot = false&minify = false:6917 :1)在MessageQueue.callFunctionReturnFlushedQueue(http:// localhost:8081/index.android.bundle?platform = android&dev = true&hot = false&minify = false:6968:1)
test.json包括:
{
"message":"hello world"
}
Run Code Online (Sandbox Code Playgroud)
该请求是成功的,当我更换'http://10.0.2.2/test.json'使用'http://facebook.github.io/react-native/movies.json',因此函数本身是好的.
'http://localhost/test.json'并且'http://127.0.0.1/test.json'都在Web浏览器中加载curl,和wget.我也尝试用127.0.0.1替换10.0.2.2.我也试过使用我的本地ip(129.xxx.xx)
要使这些请求有效,我需要做什么?如何从模拟的Android应用程序访问在localhost上运行的开发服务器?
如果您可以使用curl,请尝试该-D选项,它将标头写入文件。这样,您就可以查看两个请求是否都有 f.ex。相同的哑剧类型。
Run Code Online (Sandbox Code Playgroud)-D, --dump-header <file> Write the protocol headers to the specified file. This option is handy to use when you want to store the headers that an HTTP site sends to you. Cookies from the headers could then be read in a second curl invocation by using the -b, --cookie option! The -c, --cookie-jar option is a better way to store cookies.
| 归档时间: |
|
| 查看次数: |
962 次 |
| 最近记录: |