weixin_33691700 2015-04-01 10:39 采纳率: 0%
浏览 37

多个Ajax异步调用

I have list of lib name from the name I want fetch the author name. for this I made each loop on library name and make async call. I want return all the author name with library name once the call was end. While I try with Async call it return the call back before the call was successes. so I have try to return the callback in success but now the call back was return once the first request completed. I have try with sync call but it block the user interface. so what is the best method to do that. My code

function FetchLibAuthorID(LibCollection, callback) {
$.each(LibCollection, function (key, value) {
    $.ajax({
        url: value.MetadataURI + '?$select=schemaXml',
        type: "GET",
        headers: { "Accept": "application/json;odata=verbose" },
        success: function (data) {
            var listProperties = schemaXml2Json(data.d.SchemaXml);
            LibCollection[key].AuthorID = listProperties.Author;
            LibCollection[key].RootFolder = listProperties.RootFolder;
        },
        error: function (data2) {
            alert('error');
        }
    });
});
return callback(LibCollection);

}

  • 写回答

2条回答 默认 最新

  • weixin_33709219 2015-04-03 12:02
    关注
    var libCollection = [{ MetadataURI: "url1" }, { MetadataURI: "url2" }];
    
    function loadLibrary(complete) {
        if (libCollection.length === 0) {
            complete();
            return;
        }
    
        var lib = libCollection.pop();
        $.ajax({
            url: lib.MetadataURI + '?$select=schemaXml',
            type: "GET",
            headers: { "Accept": "application/json;odata=verbose" },
            complete: function (data) {
                var listProperties = schemaXml2Json(data.d.SchemaXml);
                LibCollection[key].AuthorID = listProperties.Author;
                LibCollection[key].RootFolder = listProperties.RootFolder;
    
                loadLibrary(complete);
            }
        });
    };
    
    // call the function
    
    loadLibrary(function() {
        alert("all libraries have been loaded")
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥20 有没有人会这个东西的
  • ¥15 cfx考虑调整“enforce system memory limit”参数的设置
  • ¥30 航迹分离,航迹增强,误差分析
  • ¥15 Chrome Manifest扩展引用Ajax-hook库拦截请求失败
  • ¥15 用Ros中的Topic通讯方式控制小乌龟的速度,走矩形;编写订阅器代码
  • ¥15 LLM accuracy检测
  • ¥15 pycharm添加远程解释器报错
  • ¥15 如何让子窗口鼠标滚动独立,不要传递消息给主窗口
  • ¥15 如何能达到用ping0.cc检测成这样?如图
  • ¥15 关于#DMA固件#的问题,请各位专家解答!