mercredi 16 mai 2018

Offline.js library Unprocessable entity

I'm trying to use Offline.js library on my Laravel 5.6 application to capture and resend Ajax requests in case of internet disruption. Laravel throws 422 HTTP error in case of a validation error with data in JSON format.

Example:

  $("#branchFrom").submit(function(event) {
   var data = $(this).serialize();
   event.preventDefault()
   $.ajax({
    url: $(this).attr('action'),
    type: 'POST',
    data: data,
    success: function(data) {
     alert("LARAVEL RESPONSE " + JSON.stringify(data));
    },
    error: function(data) {
      //Error handling done here
     var errors = JSON.stringify(data);
     alert("Crate page err handler " + errors);
    }
   });

I've have tried to use global Ajax error handler but it didn't capture the 422 error from offline.js:

$(document).ajaxError(function myErrorHandler(event, xhr, ajaxOptions, thrownError) {
 console.log("GLOBAL HANDLER SAID \n" + xhr.status + " " + JSON.stringify(thrownError) + "\n" + JSON.stringify(ajaxOptions));
  });

Offline.js offers this method and the callback method after the connection is up.

 Offline.on('up', function (data) {
            console.log("Connection is up "+data);
          });

The question is using offline.js how can I handle the errors thrown by the server after the reconnection and stored requests forwarding?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire