2

I'm just trying to store some value using rest API.

[{"userID":1,"passwordHash":"asdasd","email":"[email protected]"},{"userID":2,"passwordHash":"12345","email":"[email protected]"}]

This is what my data looks like.

I'm using Spring boot on the server side on windows machine. To post value I'm using this in the console :

curl -X POST -H "Content-Type: application/json" -d "{ \"email\": \"aszzzzzzzzzzd\", \"passwordHash\": \"sad\" }" http://localhost:8080/user

Now I'm trying to POST from my react.js application. So I'm using XMLHttpRequest()

Here is the code on my react.js application :

    var xhr = new XMLHttpRequest();
    xhr.open('POST', 'http://localhost:8080/user');
    xhr.send(JSON.stringify({ email: this.state.email,passwordHash:this.state.password }));

I've added @CrossOrigin(origins = "http://localhost:3000") to receive requests from my app.

But the problem is, it gives me an error saying :

POST http://localhost:8080/user 415

react.js http request

1 Answer 1

3

The request header wasn't formatted properly and needed to send information as a value:

xhr.setRequestHeader("Content-Type", "application/json");
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.