1

I am trying to make a POST call to a PHP file with axios.

 loginHandler = () => {

    var params = new URLSearchParams();
    params.append('User', this.state.inputUsername);
    params.append('Pwd', this.state.inputPassword);

    var callAction =  process.env.REACT_APP_API_ENDPOINT + 'LoginHandler.php';

    axios.post(callAction, params)
    .then(res => {
        const message = res.data;
        console.log(message);
    })
    .catch(function (error) {
        // handle error
        console.log(error);
      })
  }

But everything I try seems to give me a "Request aborted" error. However, doing axios requests from any other component except the LoginForm component seems to work.

Is there anything wrong with my axios call? I just copy/pasted it from a component that works, and it has no issues there.

2
  • Have you tried putting params in json axios.post(callAction, { User: this.state.inputUsername, Pwd: this.state.inputPassword }) Commented Apr 8, 2020 at 14:57
  • @YatinGaikwad yes, I tried, and I am getting the same error. Think posting the entire file would help? Commented Apr 8, 2020 at 14:59

1 Answer 1

3

The method was called on the "Submit" button onClick, which forced the page to reload before the request was completed.

Moving it to the form

 <form onSubmit ={(event) => this.loginHandler(event)} >

and adding event as parameter on loginHandler, and this line of code

event.preventDefault(); 

got it working eventually.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.