0

I want to make an post request to my BackEnd ASP.NET Core Web API from my React JS FrontEnd but when using @axios@ it is not triggered.

I followed some instructions from the internet regarding making an axios get/post request but it seemed not to work

handleRegistration = (event) => {
    event.preventDefault();

    const user = {
        email: this.state.email,
        firstName: this.state.firstName,
        lastName: this.state.lastName,
        password: this.state.password
    };

    axios.post('http://localhost:{somenumbershere}/api/values', { user })
        .then(res => {
            console.log(res);
            console.log(res.data);
        })
}

I am creating a user and want to send him as an object to my Action method to the BackEnd but it never hits the breakpoint. As you see the url I am sending my obj to, the @api/values/5@ is an example route for sending to the backend actiom method. I have decorated the Action with HttpPost, but still nothing.

Edwardcho Vaklinov
  • 118
  • 1
  • 1
  • 11

2 Answers2

0

You need to send your data using FormData object as Follows:

var formData = new FormData();
formData.append('email', this.state.email);
formData.append('firstName', this.state.firstName);
formData.append('password', this.state.password);
formData.append('password', this.state.password);

More details can be found FormData doc and in this question

0

Remove {} that wrap user inside axios.post(..., user)

herosuper
  • 164
  • 1
  • 9