Alvin
Alvin

Reputation: 109

How to access $.ajax success variables outside scope to use in othe functions?

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 () {...});

Upvotes: 3

Views: 8059

Answers (5)

ShivarajRH
ShivarajRH

Reputation: 940

I think better to create callback function:

$.post(site_url+"admin/jx_get_group_attibutes", {product_id:id}, function(rdata) {

            if(rdata.status == 'success') {
                print_link_products(rdata); // callback function
            }
            else {
                alert(rdata.result);
                return false;
            }
        }, "json");

Upvotes: 2

r15habh
r15habh

Reputation: 1468

Define variables at a place such that they are in scope of both the functions, such as

var productID;

Inside $.ajax use them as

productID = $(this).find('id').text();

Upvotes: 2

ElHacker
ElHacker

Reputation: 1687

You have to take care of the scope where the variables were defined, because you are defining your variables within the ajax success function 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 the success function 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

Upvotes: 1

Adam Merrifield
Adam Merrifield

Reputation: 10407

var productID, productName, productCurrency, productDescription, sipAccess, skypeAccess, localAccess, rechargeable;
$.ajax({
    type: "POST",
    url: getProductList,
    data: reqProductXML,
    dataType: "xml",
    contentType: "text/xml; charset=\"utf-8\"",
    success: function (data) {
        $(xml).find('product').each(function () {
            productID = $(this).find('id').text();
            productName = $(this).find('name').text();
            productCurrency = $(this).find('currency').text();
            productDescription = $(this).find('description');
            sipAccess = $(this).find('sipAccess');
            skypeAccess = $(this).find('skypeAccess');
            localAccess = $(this).find('localAccess');
            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 () {...});

Upvotes: 0

Jack
Jack

Reputation: 8941

You can change for example var productID to window.productID to make it global. Remember to take care when using global variables as naming issues can come into play.

Upvotes: 0

Related Questions