繁体   English   中英

网络请求失败 android + 反应原生

[英]Network request failed android + react native

我正在尝试从本机反应发出提取请求,它在 web 上工作正常,并按预期返回(世博会网站),但在 android 上却没有。

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

我打电话给 function 在这里提出请求:

  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
  }
}

这可以在 web 上找到,所以我想知道我是否缺少一些插件或导入或什么?

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

use url as your local ip, something like http://192.168.XX , instead of http://localhost

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM