Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery ajax success callback function definition

I want to use jQuery ajax to retrieve data from a server.

I want to put the success callback function definition outside the .ajax() block like the following. So do I need to declare the variable dataFromServer like the following so that I will be able to use the returned data from the success callback?

I've seen most people define the success callback inside the .ajax() block. So is the following code correct if I want to define the success callback outside?

var dataFromServer;  //declare the variable first  function getData() {     $.ajax({         url : 'example.com',         type: 'GET',         success : handleData(dataFromServer)     }) }  function handleData(data) {     alert(data);     //do some stuff } 
like image 977
tonga Avatar asked Feb 07 '13 15:02

tonga


People also ask

What is callback jQuery ajax?

jQuery - ajaxSuccess( callback ) Method The ajaxSuccess( callback ) method attaches a function to be executed whenever an AJAX request completes successfully. This is an Ajax Event.

What is success function ajax?

What is AJAX success? AJAX success is a global event. Global events are triggered on the document to call any handlers who may be listening. The ajaxSuccess event is only called if the request is successful. It is essentially a type function that's called when a request proceeds.

How define callback function in jQuery?

jQuery Callback Functions However, with effects, the next line of code can be run even though the effect is not finished. This can create errors. To prevent this, you can create a callback function. A callback function is executed after the current effect is finished.

What is the purpose of callback function in ajax explain with examples?

Ajax is used to create asynchronous web applications. It is deployed on the client-side and makes use of several web technologies. Ajax has made it easier to asynchronously send and retrieve data from a server without interfering with the existing page's display and functionality.


2 Answers

The "new" way of doing this since jQuery 1.5 (Jan 2011) is to use deferred objects instead of passing a success callback. You should return the result of $.ajax and then use the .done, .fail etc methods to add the callbacks outside of the $.ajax call.

function getData() {     return $.ajax({         url : 'example.com',         type: 'GET'     }); }  function handleData(data /* , textStatus, jqXHR */ ) {     alert(data);     //do some stuff }  getData().done(handleData); 

This decouples the callback handling from the AJAX handling, allows you to add multiple callbacks, failure callbacks, etc, all without ever needing to modify the original getData() function. Separating the AJAX functionality from the set of actions to be completed afterwards is a good thing!.

Deferreds also allow for much easier synchronisation of multiple asynchronous events, which you can't easily do just with success:

For example, I could add multiple callbacks, an error handler, and wait for a timer to elapse before continuing:

// a trivial timer, just for demo purposes - // it resolves itself after 5 seconds var timer = $.Deferred(); setTimeout(timer.resolve, 5000);  // add a done handler _and_ an `error:` handler, even though `getData` // didn't directly expose that functionality var ajax = getData().done(handleData).fail(error);  $.when(timer, ajax).done(function() {     // this won't be called until *both* the AJAX and the 5s timer have finished });  ajax.done(function(data) {     // you can add additional callbacks too, even if the AJAX call     // already finished }); 

Other parts of jQuery use deferred objects too - you can synchronise jQuery animations with other async operations very easily with them.

like image 134
Alnitak Avatar answered Sep 21 '22 15:09

Alnitak


Just use:

function getData() {     $.ajax({         url : 'example.com',         type: 'GET',         success : handleData     }) } 

The success property requires only a reference to a function, and passes the data as parameter to this function.

You can access your handleData function like this because of the way handleData is declared. JavaScript will parse your code for function declarations before running it, so you'll be able to use the function in code that's before the actual declaration. This is known as hoisting.

This doesn't count for functions declared like this, though:

var myfunction = function(){} 

Those are only available when the interpreter passed them.

See this question for more information about the 2 ways of declaring functions

like image 34
Cerbrus Avatar answered Sep 23 '22 15:09

Cerbrus