Sign in
Log inSign up

How to update global variable (associative array) after jQuery Ajax call?

Klevis Miho's photo
Klevis Miho
·May 25, 2016

I have this code:

var languageCode = {};
$.ajax({
  url: 'languages/en.xml',
  success: function(xml) {
    $(xml).find('string').each(function() {
      languageCode[$(this).attr('name')] = $(this).text();
      console.log(languageCode.IDSA_HI_THERE);  // This works
    });
    console.log(languageCode);
  }
});
console.log(languageCode.IDSA_HI_THERE); // This doesn't work because this gets called before the ajax request. How to make this work without using "async: false"?

Any idea?

Thanks, Klevis