本文介绍了异步获取多个json文件后触发回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有来自旧论坛的3个JSON文件,其中包含:成员,主题和回复。
现在我想通过javascript / jquery获取3个json文件在网站上呈现它。
I have 3 JSON files from an old forum containing: members, topics and replies.Now I want to render it on a website by getting the 3 json files via javascript/jquery.
我可以通过先获取成员来同步执行,当时返回获取主题并在返回时收到回复。但我想以异步方式进行。
I could do it synchronously by first getting members, when returned getting topics and when returned getting replies. But I want to do it asynchronously.
是否有类似$ .getJson的内容需要多个URL,然后返回结果数组?就像一个富有想象力的$ .getJson([url1,url2,url3],callBackFunction)
Is there anything like $.getJson that takes multiple URLs and then return an array of results? Like an imaginative $.getJson([url1,url2,url3],callBackFunction)
推荐答案
使用:
var A = $.getJSON(url1);
var B = $.getJSON(url2);
var C = $.getJSON(url3);
$.when(A,B,C).done(function(aResult, bResult, cResult){//when all request are successful
console.log([aResult[0],bResult[0],cResult[0]]);
});
这篇关于异步获取多个json文件后触发回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!