Home > Back-end >  Reading to JSON URLs with jQuery
Reading to JSON URLs with jQuery

Time:12-17

I need to read two JSON URLs with jQuery, mangle them together and then proceed updating my UI from the JSON data.

With the two URLs in URLA and URLB and a global variable globalDataA I tried:

function useBothData(dataB) {
   // use dataB and globalDataA
};

function useDataA (data) {
    globalDataA = data
    $.getJSON (URLB, useBothData);
};

$.getJSON (URLA, useDataA);

The idea is that after loading the 1st URL I load the 2nd and then work with them. However this does never seem to return. (I get the Brwoser warning "Script takes too long".)

How can I arrange jQuery's getJSON calls for the two URLs (and their success functions), so that I can then use their data in finally one function?

CodePudding user response:

You can use the callback function of getJSON

$.getJSON(URLA, useDataA, function(dataB) {
  //useBothData = globalDataA   dataB

  $.getJSON(URLB, useBothData, function(result) {
    //do more stuff
  })
});
  • Related