我正在尝试在我的第一个react-native应用程序(expo项目,在我的物理android设备上运行它)中发出GET API请求。我在尝试使用Axios发出获取请求时遇到麻烦-我的后端是快速连接到Firebase DB。{以下设置在React Web应用程序中正常工作}
...
const handleSubmit = () => {
const userData = {
email: email,
password: password
};
axios.get('/alltests')
.then((res) => {
alert("Successfully loggedin", res)
console.log(JSON.stringify(res.data))
})
.catch((err) => {
console.log("\n\nI'm in error block - frontend")
console.log(err)
alert(err);
//setErrors(err.response.data)
})
...
我的package.json看起来像->
"dependencies": {
"@material-ui/core": "^4.8.1",
"@material-ui/icons": "^4.5.1",
"axios": "^0.19.0",
"cors": "^2.8.5",
"expo": "~36.0.0",
"react": "~16.9.0",
"react-dom": "~16.9.0",
"react-native": "https://github.com/expo/react-native/archive/sdk-36.0.0.tar.gz",
"react-native-paper": "^3.4.0",
"react-native-vector-icons": "^6.6.0",
"react-native-web": "~0.11.7"
},
"devDependencies": {
"babel-preset-expo": "~8.0.0",
"@babel/core": "^7.0.0"
},
"private": true,
"proxy": "https://asia-something.net/api"
}
但是,如果我使用axios.get('https://asia-something.net/api/alltests')这似乎在代码中工作。
我没有使用localhost或在本地服务器上运行后端。有人可以帮我吗?
例如,尝试使用代理服务器的完整URL,假设您的代理服务器网址是:http://localhost:3000/alltest
。
而不是使用localhost
,而是使用本地计算机IPV4 / inet IP作为完整的请求URL,看起来像这样:http://192.168.1.4:3000/alltest
您可以通过从命令提示符/终端运行ipconfig(windows)
ifconfig(mac/linux)
来找到计算机的本地IP(192.168.1.4)IPV4 / inet地址>>