Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
不使用React Native从本地主机获取JSON数据_Json_Reactjs_React Native_React Native Android - Fatal编程技术网

不使用React Native从本地主机获取JSON数据

不使用React Native从本地主机获取JSON数据,json,reactjs,react-native,react-native-android,Json,Reactjs,React Native,React Native Android,这可能是一个重复的问题,但我尝试了大多数来自互联网的东西,但没有成功。 我是个新来的本地人。我正在使用物理设备(我的android手机)作为运行我的输出,并试图从我的一个测试项目中获取JSON数据,该项目在localhost中用PHP编码(默认端口80) 下面是我的Login.js登录函数,用于获取数据: login = () => { fetch('http://localhost/XTest/send_react', { method: 'POST',

这可能是一个重复的问题,但我尝试了大多数来自互联网的东西,但没有成功。 我是个新来的本地人。我正在使用物理设备(我的android手机)作为运行我的输出,并试图从我的一个测试项目中获取JSON数据,该项目在localhost中用PHP编码(默认端口80)

下面是我的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 load中得到的只是:

网络请求失败,错误代码串为红色


我尝试更改我的url=>'http:/localhost:80/xxxxxxxxx',甚至按照建议在internet页面的某个地方添加了“proxy”:“”,但没有成功。如有任何帮助或建议,将不胜感激。多谢各位

这是因为localhost仅是设备的本地主机。 您需要运行PHP的实际设备的IP地址(如果它在同一网络上,它可能看起来像192.168.x.x),也可以在终端或命令提示符中尝试
apr-a
,查看网络上所有设备的地址。

fetch('http://YOURIP(192.168.X.X):你的端口'{
方法:“POST”,
标题:{
“接受”:“应用程序/json”,
“内容类型”:“应用程序/json”,

},
对于Android Emulator(Genymotion)面临此问题的任何人,只需确保在端口号之后使用本地
IP地址即可。使用本地终端或命令提示符中的
ipconfig
即可轻松识别该地址

我的React Native中匿名函数的示例

login = () => {

fetch('http://192.x.x.x:PORT', {
  method: 'POST',
  headers: { 'content-type': 'application/json' },
  body: JSON.stringify({ 
      username: this.state.username, 
      password: this.state.password }),
}).then(res => {
  console.log(res)
}).catch(err => {
  console.log(err)
})
  ...etc 
}

我也有同样的问题,任何人都可以找到解决方案。我试图使用IP地址,但没有给出响应。请查看我下面的代码

let email = this.state.email;
let pass = this.state.pass;

fetch("http://192.168.1.2:8080/ReactAPI/post.php", {
      method: "POST",
      body: JSON.stringify({
        email: email,
        password: pass
      }),
      headers: {
        "Content-Type": "application/json",
        Accept: "application/json"
      }
    })
      .then(response => console.log(response))
      .catch(err => console.log(err));

您可以在特定的IP上启动服务器

例如,对于json服务器,-host[IP地址]将在该位置启动服务器:

json-server --watch db.json -p 3001 -d 2000 --host 192.168.83.230