Save REQUESTS AJAX to be executed later

Asked

Viewed 273 times

2

Use jQuery and the Ajax library to make REQUESTS.

There is a way to save REQUESTS, follow the example..

The user is there moving and suddenly the connection drops and the user does not notice it. And continues doing what he did.. and suddenly it sends a request, obvious this request will not be sent but how to save this request for when the internet comes back it is sent and the user does not lose its content..

This REQUEST has to be saved and immune to page refresh, so that even if it closes the page, when it returns the pending requests will be sent.

  • friend, it is even possible to do this with localStorage, you could do a wrapper for the call $.ajax with timeout, and in the error of $.ajax you would put the request in a fila in the localStorage, then you would have a window.setInterval reading this fila and resending the request, but I’ll tell you right away that this approach will bring you more problems than benefits.

  • @Tobymosque why it would cause more errors than solutions ?

  • Imagine a problem the transport channel on the return of the request, to the server everything is ok, but the browser received no response, so in this case you would forward the request. this may end up generating data duplicity, another point, the user sends a form, gave timeout, but as you put the request in the pool you gave the user a "OK" message, however before the connection was reestablished the user cleaned the localStorage, then we have a success message in an error scenario.

  • then perhaps it is better, just report an error in communication, then the user can perform some query in the system or resend the data.

  • @Tobymosque I understand, but this page would be for offline use (Manifest cache) so that would be essential.. I will try to think of something.

  • In this case, I think the ideal would be to develop a Chrome app, I believe this way you can use a sqlite database, then you would have a "service" doing the data synchronization when the connection is available.

  • if it’s not a large amount of data, you can save it to cookies.. Another place to save is localstorage, but available in browsers with newer versions only.

Show 2 more comments

1 answer

0

You can log your application, below an example of how to do this:

$(function() {

  $('#submit_button').on('click', function() {
    var data = {teste: 'teste'};
      $.ajax({
         url: "send_data.php",
         data: data,
         method: "POST",
         dataType: "json",
         beforeSend: function( data ) {
             $('#msg').html("enviando dados...");
         }
      }).done(function( data ) {
             var msg = $.parseJSON(data);
             $('#msg').html(msg);
      }).fail(function(error) {
         //redireciona e salva o log do erro
         window.location.href='event_log_register.php?data_expected=data:,' + 
         data.responseJSON +
         '&data_error=' +
         error.responseJSON +
         '&redirect=true';

      }).always(function() {
         $('#msg').html("Envio concluído!");
      });
   });
});

Copy and paste the text below into your browser:
data:application/octet-stream;Base64,Sgvsbg8=

Browser other questions tagged

You are not signed in. Login or sign up in order to post.