简体   繁体   English

使用 jQuery 读取 JSON URL

[英]Reading to JSON URLs with jQuery

I need to read two JSON URLs with jQuery, mangle them together and then proceed updating my UI from the JSON data.我需要使用 jQuery 读取两个JSON URL,将它们组合在一起,然后从 JSON 数据继续更新我的 UI。

With the two URLs in URLA and URLB and a global variable globalDataA I tried:使用URLAURLB中的两个 URL 和一个全局变量globalDataA我尝试了:

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.这个想法是,在加载第一个 URL 后,我加载第二个,然后使用它们。 However this does never seem to return.然而,这似乎永远不会回来。 (I get the Brwoser warning "Script takes too long".) (我收到 Brwoser 警告“脚本耗时过长”。)

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?如何为两个 URL(及其成功函数)安排 jQuery 的getJSON调用,以便我可以在最后一个function 中使用它们的数据?

You can use the callback function of getJSON可以使用getJSON的回调function

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM