jquery - Multiple Ajax Async call -
i have list of lib name name want fetch author name. made each loop on library name , make async call. want return author name library name once call end. while try async call return call before call successes. have try return callback in success call return once first request completed. have try sync call block user interface. best method that. 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);
}
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 function loadlibrary(function() { alert("all libraries have been loaded") });
Comments
Post a Comment