N
N
Nymeria182021-12-25 14:43:58
AJAX
Nymeria18, 2021-12-25 14:43:58

How to cancel page reload on AJAX request 'DELETE' and 'POST' to Json-server?

Hello! I hope for your help, since I have already searched the entire Internet, I can not find a solution to the problem.
Making an AJAX DELETE or POST request using the JQuery $.ajax() method to a fake REST API server created with a JSON server. So, the request is working correctly: the specified data is deleted from the server (and the json itself is updated), but after that the page is reloaded !! How to cancel this reboot?? The point of the whole task is to create a SPA with support for CRUD operations on the server, so there should not be a reboot. preventDefault, return false don't help.
I would be grateful for any ideas and guesses in resolving the issue. I am attaching the code.

<section id="app">
    <button type="button" class="button">Click</button>
  </section>

$(".button").click(del);

function del (e) {
  console.log('del');
  e.preventDefault();
  $.ajax({
    url: 'http://localhost:3001/posts/1',
    method: 'delete',
    dataType: 'html',
    success: function(data){
      console.log(data)
    }
  });
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question