我正在开发一个使用React Native的Android应用程序。我正在Galaxy Nexus API 23上测试我的应用程序,该应用程序在Android Studio中模拟,并在Mac上运行。React Native在Android上,ajax调用与提取失败
我希望允许用户互相共享信息。我使用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);
});
}
};
通过AjaxEngine.test();
叫我在模拟器我收到以下错误:
test.json包括:
{
"message":"hello world"
}
当我用'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.x.x)
我需要做些什么才能使这些请求有效?我如何从模拟的android应用程序访问在本地主机上运行的开发服务器?