V
V
Vizi20042021-12-24 12:07:32
JSON
Vizi2004, 2021-12-24 12:07:32

The page is reloaded when a post request is sent to the json-server. How to fix?

I made a front on react, I use json-server for the database. So here is an application like an online store. When the add button is clicked, the function is called

let pushToCart = async (obj) => {
    await axios.post(`https://61252c323c91fb0017e729af.mockapi.io/CartItems`, obj);
  };

Everything is fine, the object is added to db.json, but the page is reloaded

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
shsv382, 2021-12-27
@shsv382

Try adding preventDefault to the onClick handler:

event.preventDefault();
pushToCart(obj);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question