Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery ajax success anonymous function scope

Tags:

jquery

ajax

How do I update the returnHtml variable from within the anonymous success function?

function getPrice(productId, storeId) {     var returnHtml = '';      jQuery.ajax({         url: "/includes/unit.jsp?" + params,         cache: false,         dataType: "html",         success: function(html){             returnHtml = html;         }     });      return returnHtml; } 
like image 618
Shaun Avatar asked Sep 22 '09 01:09

Shaun


People also ask

What triggers 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.

What is the use of success function in AJAX?

Definition and Usage The ajaxSuccess() method specifies a function to be run when an AJAX request is successfully completed.

What is difference between success and complete in AJAX?

success() only gets called if your webserver responds with a 200 OK HTTP header - basically when everything is fine. However, . complete() will always get called no matter if the ajax call was successful or not - maybe it outputted errors and returned an error - . complete() will still get called.

What is success and error in AJAX?

jquery ajax calls are asynchronous by default. So success & error functions will be called when the ajax load is complete. But your return statement will be executed just after the ajax call is started.


2 Answers

That is the wrong approach. The first A in AJAX is Asynchronous. That function returns before the AJAX call returns (or at least it can). So this is not an issue of scope. It is an issue of ordering. There are only two options:

  1. Make the AJAX call synchronous (not recommended) with the async: false option; or
  2. Change your way of thinking. Instead of returning HTML from the function you need to pass a callback to be called when the AJAX call succeeds.

As an example of (2):

function findPrice(productId, storeId, callback) {     jQuery.ajax({         url: "/includes/unit.jsp?" + params,         cache: false,         dataType: "html",         success: function(html) {             // Invoke the callback function, passing the html             callback(productId, storeId, html);         }     });      // Let the program continue while the html is fetched asynchronously }  function receivePrice(productId, storeId, html) {     // Now do something with the returned html     alert("Product " + productId + " for storeId " + storeId + " received HTML " + html); }  findPrice(23, 334, receivePrice); 
like image 77
cletus Avatar answered Oct 03 '22 14:10

cletus


Short answer, you can't, the first A in AJAX stands for Asynchronous, which means the request is still going when you get to the return statement.

You can do it with a synchronous (non-async) request, but it's generally a Bad Thing

Something like the following oughta return the data.

function getPrice(productId, storeId) {   var returnHtml = '';    jQuery.ajax({     url: "/includes/unit.jsp?" + params,     async: false,     cache: false,     dataType: "html",     success: function(html){       returnHtml = html;     }   });    return returnHtml; } 

BUT

Unless you really really need to be able to use the return value from test straight away, you'll be much better off passing a callback into test. Something like

function getPrice(productId, storeId, callback) {   jQuery.ajax({     url: "/includes/unit.jsp?" + params,     async: true,     cache: false,     dataType: "html",     success: function(html){       callback(html);     }   }); }  //the you call it like getPrice(x,y, function(html) {     // do something with the html } 

Edit Sheesh, you guys are quicker to say what I said :-)

like image 39
Dan F Avatar answered Oct 03 '22 13:10

Dan F