Jquery ajax not working in firefox

I have multiple ajax and I handle them on completion and failure.

$.when(

   $.ajax({
        url: "/ajax",
        type: "POST",
        data: {
             _xsrf: xsrf_token,
             'action': "/document/x",
             'values': JSON.stringify(values)
             }
      }).done(function() {
           console.log("done!");
           window.location.reload();
      }).fail(function(jqXHR, textStatus) {
           console.log(jqXHR);
           $('#error').text(textStatus);
  })
).then(function() {

 // All have been resolved (or rejected)

 });

      

In chrome and IE, when the ajax is successful, it ends up complete and shows a message that the call was successful and the page was reloaded. In FF, if the call is successful, it first fails and it ends. Any idea?

change

This behavior only happens in a specific case: I am trying to delete as well as add the same user to the database with two asynchronous calls in $ .when: which from the user side is possible, but asynchronous calls are handled differently in different browsers.

+2


source to share


1 answer


I think you are using jQuery.when () incorrectly because this method is part of Deferred , which implements the Promise interface , and the jqXHR object returned by jQuery.ajax () implements the Promise interface , giving them all the properties, methods and behavior of the Promise ( see Deferred Object for more information)

A more correct way to write the previous code could be as follows:

var promise = $.ajax({
  url: "/ajax",
  type: "POST",
  data: {
    _xsrf: xsrf_token,
    action: "/document/x",
    values: JSON.stringify(values)
  }
});

promise.done(function () {
  console.log("done!");
  window.location.reload();
});

promise.fail(function (jqXHR, textStatus) {
  console.log(jqXHR);
});

      

Or if you want to use jQuery.when ()



$.when(
  $.ajax({
    url: "/ajax",
    type: "POST",
    data: {
      _xsrf: xsrf_token,
      action: "/document/x",
      values: JSON.stringify(values)
    }
  })
).then(
  function done() {
    console.log("done!");
    window.location.reload();
  },
  function fail(jqXHR, textStatus) {
    console.log(jqXHR);
  }
);

      

I recommend you read the links provided.


Happy reading and happy chatting!

+2


source







All Articles