Before posting this I’ve read through but I’m still not clear on how to achieve this.
I’m receiving an xml file and setting some variable, how can I access all of the variables outside the ajax success to use in other functions?
$.ajax({
type: "POST",
url: getProductList,
data: reqProductXML,
dataType: "xml",
contentType: "text/xml; charset=\"utf-8\"",
success: function (data) {
$(xml).find('product').each(function () {
var productID = $(this).find('id').text();
var productName = $(this).find('name').text();
var productCurrency = $(this).find('currency').text();
var productDescription = $(this).find('description');
var sipAccess = $(this).find('sipAccess');
var skypeAccess = $(this).find('skypeAccess');
var localAccess = $(this).find('localAccess');
var rechargeable = $(this).find('rechargeable').text();
$('#urProductSelect').append("<option value='" + productID + "'>" + productName + "</option>");
});
}
}); //End ajax
// I need to use the variable out here....
$('#urProductSelect').change(function () {...});
You have to take care of the scope where the variables were defined, because you are defining your variables within the ajax
successfunction all the variables belong to that scope, so any other function defined outside that function won’t be able to “see” that variables, you can define them outside thesuccessfunction within a scope that any other function will be able to “see” them like the global scope but that is kind of messy. Take a look at this book, It will show you some good examples about scope and closures too:http://jqfundamentals.com/book/index.html#example-2.44