Home > Back-end >  Axios POST doesn't send data (object)
Axios POST doesn't send data (object)

Time:08-07

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).

CodePudding user response:

  1. Be sure after sending object that there is some values in it

  2. Check if you dont have any typo

  3. Sometimes problem can be using slash '/' after the end try without slash

    .post("http://localhost:8080/customers", customerData)

  4. Check your logs in console

  5. Try using other clients to send data for example REST Client in vscode

https://marketplace.visualstudio.com/items?itemName=humao.rest-client

CodePudding user response:

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.

  • Related