Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any analog to a 'finally' in jQuery AJAX calls?

Is there a Java 'finally' analogue in jQuery AJAX calls? I have this code here. In my always I throw an exception, however I ALWAYS want it to go to the then() method.

    call.xmlHttpReq = $.ajax({         url : url,         dataType : 'json',         type : 'GET'     }).always(function(processedDataOrXHRWrapper, textStatus, xhrWrapperOrErrorThrown) {         throw "something";      }).then(function() {          alert("i want to always run no matter what");     }); 

I have tried to use done(), complete(), and the another always(), but nothing seems to work.

Here is JSFiddle :

http://jsfiddle.net/qv3t3L0m/

like image 714
Oliver Watkins Avatar asked Apr 10 '13 12:04

Oliver Watkins


People also ask

Are AJAX calls asynchronous?

Ajax is a very well known method for loading the content of a Web page without manually refreshing it. But the letter “A” in Ajax means asynchronous, meaning that you need to have a callback function that will return the results.

Which is better fetch or AJAX?

Fetch is compatible with all recent browsers including Edge, but not with Internet Explorer. Therefore, if you are looking for maximum compatibility, you will continue to use Ajax to update a web page. If you also want to interact with the server, the WebSocket object is also more appropriate than fetch.

Is AJAX successful deprecated?

Yes, it is deprecated in jQuery 1.8 onwards.

Do AJAX calls pass cookies?

AJAX calls only send Cookies if the url you're calling is on the same domain as your calling script. This may be a Cross Domain Problem.


1 Answers

See this example:

$.ajax({         type: "GET",         dataType: dataType,         contentType: contentType,         async: TRUE,         url: $('html form:nth-child(1)').attr('action') + "?" $('html form:nth-child(1)').serialize(),         success: function(data) {             console.log("FUNFOU!");         },         error: function(data) {             console.log("NÃO FUNFOU!");         },         complete: function(data) {             console.log("SEMPRE FUNFA!");              //A function to be called when the request finishes              // (after success and error callbacks are executed).          }     }); 

For more informations: http://api.jquery.com/jquery.ajax/

like image 154
Rafael Gomes Francisco Avatar answered Sep 24 '22 13:09

Rafael Gomes Francisco