Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.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 React native axios工作不正常_React Native - Fatal编程技术网

React native React native axios工作不正常

React native React native axios工作不正常,react-native,React Native,我身上有这条路线 app.get('/welcomeRoute',(req,res)=> { res.send("Hello world") }) app.listen(4000,()=> { console.log("Server is up") }) 我用npm I axios在我的react原生项目上安装了axios,我用expo init创建了我的项目 以下是我如何使用路线 test = () => { a

我身上有这条路线

app.get('/welcomeRoute',(req,res)=> {
    res.send("Hello world")
})


app.listen(4000,()=> {
    console.log("Server is up")
})
我用
npm I axios
在我的react原生项目上安装了axios,我用
expo init
创建了我的项目

以下是我如何使用路线

test = () => {
 axios.get('http://localhost:4000/welcomeRoute').then((res)=> {
   console.log('--------res', res);
 }).catch((err)=> {
   console.log('--------err', err);
 })
}

每当我调用此路由时,我总是得到
----err[错误:网络错误]
。 我在
邮递员
中测试了这条路线,效果很好


有什么建议吗?

如果问题出在Android上,解决方案是在您的AndroidManifest.xml文件中添加以下内容

android:usesCleartextTraffic="true"
在IOS中,默认情况下会阻止未加密的网络请求,即https将起作用,http将不起作用

您需要通过IP地址从模拟器/设备访问服务器。而不是http://localhost:port 你应该使用http://your_ip_address:port


实际上,在React Native中,您最好给出系统的ip地址,而不是本地主机,因为设备使用localhost无法识别您的ip地址

我使用expo init创建了我的应用程序,因此我没有任何androidManifest文件:(而不是您应该使用ittry
https
,而不是http可能是它的工作方式。