2

i have multiple ajax and i handle on done and on fail.

$.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 succesfull it ends in done, and shows some message that the call was sucessfull and the page is reloaded. In FF if the call is succesfull it goes first in fail and the it goes in done .. any idea?

edit:

This behaviour is only in a specific case: I am trying to remove and also add the same user in the database in two asynchron calls in the $.when: which from user side is possible, but the asynchron calls are handled different in the different browsers.

8
  • I see some Syntax Errors. Please update code Commented Jun 15, 2015 at 14:39
  • you're misusing $.when. that code should be throwing syntax errors in all browsers, not just firefox. copy paste error? Commented Jun 15, 2015 at 14:42
  • and the downvote is because? Commented Jun 15, 2015 at 14:46
  • your question was unclear due to the, i'm assuming, copy paste failure. Commented Jun 15, 2015 at 14:56
  • Now i still find it unclear, because it isn't possible for both done and fail to be called, as described in your question. Commented Jun 15, 2015 at 14:57

1 Answer 1

2

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

A more proper way to write the previous code could be as follow:

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 encourage you to read the links provided.


Happy reading and happy conding!

Sign up to request clarification or add additional context in comments.

4 Comments

it is working, tnx for the links, appreciate your help!
but not for FF. I still have the problem.
in FF then() gets called before when() !! very strange and unexpected!
I can confirm that your solution is not working using FF 41.0 on Windows 7.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.