我正在读取Ajax函数中XML文件中的图像URL,异步设置为false,因此它应该等待Ajax函数完成。在接收这些URL时,我正在调用另一个函数来验证URL是否有有效图像,并且我从回调函数“isUrlIsImage()”接收布尔值...但是我发现Ajax不等待从回调函数接收所有结果......谁都知道在哪里看?如何强制Ajax等待所有回调函数......非常感谢提前...
$.fn.initializeImg = function (CP_ID) {
var selected_dataType = "";
if (currentBrowser().browser == "IE" && currentBrowser().version < 10) {
selected_dataType = "text";
}
else {
selected_dataType = "xml";
}
$.ajax({
type: "GET",
url: "XML_file.xml",
dataType: selected_dataType,
async:false,
success: function (xml) {
var processedXML_01 = parseXml(xml);
$(processedXML_01).find('property').each(function () {
var j_propertyIn_List = $(this).find('propcode').text();
if (j_propertyIn_List == gb_var.j_propcode) {
$(this).find('photo').each(function (index) {
gb_var.j_propertyImgURLs[index] = $(this).find('urlname');
console.log(">>>> "+gb_var.j_propertyImgURLs[index].text());
isUrlIsImage(gb_var.j_propertyImgURLs[index].text(), function (_url, result) {
//alert("check result >> "+result + " for " + _url);
if (result) {
console.log("******TRUE********* check result >> " + result + " for " + _url);
}
else {
console.log("******FALSE********* check result >> " + result + " for " + _url);
}
});
});
}
});
},
error: function () {
alert("An error occurred while processing XML file.");
}
});
} //end plugin function
})(jQuery);
function isUrlIsImage(given_url, callback) {
console.log(" checking............. " + given_url);
url = given_url;
$("<img>", {
src: url,
error: function () { callback(url, false); },
load: function () { callback(url, true); }
});
}