简体   繁体   中英

How to fix fetch returning 404 when reaching for a php file in react native?

I am currently learning React Native. In order to do it, I'm trying to make a log in screen. I have only a login screen right now, and I want to connect it with a .php file that will connect to a database and check for the user and password. I usually use XMLHttpRequest for this connection, but someone told me that it's better to use fetch on React Native (and regardless, both ran into the exact same problem: the status of the request is always 404 and it never seems to find the .php file.

logInProcess  = () => {  
    var respond = "";
    var logInValues = {
      login: this.state.user,
      password: this.state.password
    }

    fetch('./backend/login.php', {
      method: "POST",
      body: JSON.stringify(logInValues)
    }).then( (response) => {
        respond = response.json();
        console.log(response.status)
        console.log(respond);
      }
    )
  };

Right now, this code is merely supposed to showcase the connection to the database working. I'll only add further stuff to it later, but right now, I need to fix this 404 problem.

The problem is you have used "./" instead of "/"

logInProcess  = () => {  
var respond = "";
var logInValues = {
  login: this.state.user,
  password: this.state.password
}

fetch('/backend/login.php', {
  method: "POST",
  body: JSON.stringify(logInValues)
}).then( (response) => {
    respond = response.json();
    console.log(response.status)
    console.log(respond);
  }
)
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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