0

In my NodeJS app, I am trying to keep the (data) value that application sends with res.send() after ajax request is .done() successfully with the form data submission.

ajax Request

$.ajax({
      url: "/users",
      type: "POST",
      contentType: "application/json",
      data: JSON.stringify(payload)
  })

  .done(function (data, status, jqXHR) {
    window.location.reload();
    $("#output").html(data) // User saved in Database with ID: xxx
  });

  .fail(function(data) {
    console.log( "error" );
  });

  .always(function() {
    console.log("processed")
  });

The (data) value (which is sent by the back-end) is being projected as a message in the #output on .done() but the window.location.reload() reloads the page (as it should) and wipes out from existence the message I need to show after the page reloads.

How can it happen to transfer the (data) value so I can project it after the page reloads ?

Obviously I do not want to put it on an alert() or create a delay timer for the reload.

Cadmos
  • 277
  • 4
  • 21
  • You will need to leverage browser storage such as [`localStorage`](https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage). Once a page reloads, its context is lost. – zero298 May 14 '18 at 17:09
  • Possible duplicate of [Persist variables between page loads](https://stackoverflow.com/questions/29986657/persist-variables-between-page-loads) – zero298 May 14 '18 at 17:11
  • 1
    For what reason you are reloading that page,Ajax is used for that only to not reload whole page and puts data easily without any reloads or reload any div of your content. $("#div").load(location.href+" #div>*",""); – Mehta Harshit May 14 '18 at 17:26

0 Answers0