Afnan
Afnan

Reputation: 53

i am not able to send data throught axios

Not able to get data through axios . below is the front end code

function sendData(newData) {
    axios.post("/cart/addData",newData).then((res) => {
        alert(res)
    });
}

let getBtn = document.getElementById("cartButton")
getBtn.addEventListener("click", function() {
    let data = getBtn.dataset.cart;
    let newData = JSON.parse(data);
    sendData(newData);
});

when I try to get it on server side it always shows req.body to be an empty object

router.post("/addData", (req, res) => {
    console.log(req)   
});

Upvotes: 0

Views: 35

Answers (1)

Naor Levi
Naor Levi

Reputation: 1813

Add a middleware at the backend to parse the body:

app.use(express.json());

Upvotes: 2

Related Questions