Axios POST doesn't send data (object)

Issue

I’m trying to send the following object from my REACT app to my node.js express server via axios.post:

const customerData = {
  firstName: enteredFirstName,
  lastName: enteredLastName,
  address: enteredAddress,
  phone: enteredPhone,
  ccn: enteredCCN,
  username: enteredUsername,
  password: enteredPassword,
  email: enteredEmail,
  role: "customer",
};

This is my code which i use to send the object:

axios
  .post("http://localhost:8080/customers/", customerData)
  .then((res) => {
    console.log(res.data);
  })
  .catch((err) => console.log(err));

The request is sent, but the data object isn’t recieved by the server (but the request is), and if i use JSON.stringify on my object, it does deliver it, only as a string, which isn’t what i want (CORS are open).

Solution

You can check in the browser’s network tab to see if the data is present in your request. If not, it’s the problem of the client site. Otherwise, you probably miss the middleware to parse JSON data on the server. –
Đăng Khoa Đinh

this was correct – use(json()) wasn’t implemented correctly.

Answered By – Boris

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published