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; }
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.
Definition and Usage The ajaxSuccess() method specifies a function to be run when an AJAX request is successfully completed.
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.
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.
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:
async: false
option; orAs 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);
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 :-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With