My FeedDiscussionsHashnode Enterprise
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more

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