【发布时间】:2018-06-17 12:09:06
【问题描述】:
这可能是重复的问题,但我尝试了互联网上的大部分内容,但没有成功。 我是 React Native 的新手。我正在使用物理设备(我的 android 手机)作为运行我的输出,并尝试从我在本地主机(默认端口 80)中用 PHP 编码的测试项目之一获取 JSON 数据。
这是我的 Login.js 获取数据的登录函数:
login = () => {
fetch('http://localhost/XTest/send_react', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
username: this.state.username,
password: this.state.password,
})
})
.then((response) => response.json())
.then((res) => {
if (res.success === true) {
AsyncStorage.setItem('user', res.user);
this.props.navigation.navigate('Profile');
} else {
alert(res.message);
}
})
.done();
我通过加载上面的 url 测试了我的 PHP 代码,它提供了 JSON 数据。但是我在 React Native 负载中得到的只是:
网络请求失败,出现一堆红色的错误代码。
我尝试更改我的 url => 'http:/localhost:80/xxxxxxxxx' 甚至添加了 "proxy": 'http://localhost/' 按照互联网页面某处的建议,但没有任何成功。任何帮助或建议将不胜感激。谢谢。
【问题讨论】:
标签: json reactjs react-native react-native-android