user1937021 user1937021 - 5 months ago 13
jQuery Question

store response from $.get in a variable to use later

I'm retrieving a response from a php with $.get, but how do I store the response in a variable so I can use it later in my code? Thanks!

$.get('promo_getstate.php', {
email: emailaddress,
country: 'DE',
lang: lang,
source: '1304_Spring_dly'
}, function (data) {
console.log('data is' + data);
getstate = data;
});

Answer

just declare the variable you want to set outside of the .get call itself. then it will be accessible inside of the callback function where you want to set it to the returned data.

var getstate = "";
$.get('promo_getstate.php', {
        email: emailaddress,
        country: 'DE',
        lang: lang,
        source: '1304_Spring_dly'
            }, function (data) {
                console.log('data is' + data);
                getstate = data;
             });