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可能是它的工作方式。