Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
172 views
in Technique[技术] by (71.8m points)

Network request failed android + react native

I am trying to make a fetch request from react native, It works fine on web and returns as expected (expo web) but not on android.

I keep getting 'Network request failed' and i cant see why as its working on web. I have also tried to change the status code from a 414 to a 200 on my server so that there would be no error however this just meant no response object still but without errors.

I am calling a function that makes the request here:

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

and this is where my request is being made from

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

This works find on web so i wonder if im missing some plugin or import or something?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The issue was i was using a localhost url. I have run the server on ngrok so i have a public link that i can use anywhere


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...