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")
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵