我有我希望获取作者姓名的名称的lib名称列表。为此,我在库名称上进行了每个循环并进行异步调用。我希望在呼叫结束后,使用库名称返回所有作者姓名。 当我尝试使用Async调用时,它会在调用成功之前返回回调。所以我试图成功返回回调,但现在回复是在第一个请求完成后返回的。 我尝试过同步调用,但它阻止了用户界面。那么最好的方法是什么呢。我的代码
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);
}
答案 0 :(得分:0)
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")
});
答案 1 :(得分:0)
function FetchLibAuthorID(LibCollection, callback) {
var count=0;
$.each(LibCollection, function (key, value) {
count++;
$.ajax({
url: value.MetadataURI + '?$select=schemaXml',
type: "GET",
headers: { "Accept": "application/json;odata=verbose" },
success: function (data) {
count--;
var listProperties = schemaXml2Json(data.d.SchemaXml);
LibCollection[key].AuthorID = listProperties.Author;
LibCollection[key].RootFolder = listProperties.RootFolder;
},
error: function (data2) {
alert('error');
}
});
});
while(count>0)
{
}
return callback(LibCollection);
}