网络请求失败android+react native



我正在尝试从react native发出获取请求,它在web上运行良好,并按预期返回(expo-web(,但在android上没有。

我一直收到"网络请求失败",我不明白为什么,因为它在网络上工作。我还尝试将服务器上的状态代码从414更改为200,这样就不会出现错误,但这只是意味着仍然没有响应对象,但没有错误。

我正在调用一个在此处发出请求的函数:

async function handleSubmit() {
try {
const res = await createAccountService({name: name.value,email:email.value,password:password.value})

console.log(res);
}catch(e){
console.log(e);
// alertFunction('Couldnt create account', res.error.message)
}

这就是向我提出请求的地方

export const createAccountService = async({name, email, password}) => {
try {
const timestamp = Math.floor(Date.now() / 1000);
// const requestObject = {name, email, password, timestamp}
const requestObject ={
name: "afdsf",
email:"s7edsg@t.com",
password:"123423569",
timestamp: 1607548529
}
const unparsedResponse = await fetch(URL + "/account/createaccount", {
method: 'POST', 
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(requestObject)
})


console.log(unparsedResponse);
const response = await unparsedResponse.json()
if(response.error){
// Error - Return this to the alert
}else{
// All good - safe this to the user state
}

return response
}catch(e){
console.log('this is being hit');
throw e
}
}

这项工作可以在网上找到,所以我想知道我是否缺少一些插件或导入或其他什么?

问题是我使用的是localhost url。我已经在ngrok上运行了服务器,所以我有一个公共链接,我可以在任何地方使用

使用url作为本地ip,类似于http://192.168.X.X而不是http://localhost

最新更新