我正在进行一个解析 xml 文件的 ajax 调用。我已将 async 设置为 false,但它仍然不起作用。我在 ajax 调用之前和之后收到了警报,它似乎连续工作。谁能告诉我我哪里出错了。这是我正在做的代码段
var i = 0;
var xmlLength = 0;
var arr = new Array();
alert("1");
$.ajax({
type: "GET",
url: "sample.xml",
async: false,
dataType: "xml",
success: function (xml) {
$(xml).find('CD').each(function () {
alert("2");
i = i + 1;
xmlLength += 1;
var title;
var getTo;
var artist;
var getToNode;
var $allToElements = 'TO';
var country = $(this).find('COUNTRY').text();
getTo = $(this).find('TO'); //.text();
if (getTo.length > 1) {
var $divId = "window" + i;
$('<div class="window" id="window' + i + '" />')
.append('<div class="topright" id="topright' + i + '"/>')
.append('<div class="topleft" id="topleft' + i + '"/>')
.append('<div class="bottomright" id="bottomright' + i + '"/>')
.append('<div class="bottomleft" id="bottomleft' + i + '"/>')
.appendTo('div#demo1');
var countr = $(this).find('COUNTRY').text();
$('div#topright' + i).append(countr);
$('div#topleft' + i).append('NW');
$('div#bottomleft' + i).append('SW');
$('div#bottomright' + i).append('SE');
var j = 0;
$(this).find('TO').each(function () {
var $name = $(this).text();
j += 1;
$('div#window' + i).append($('<p id="to' + i + '">').text($name));
});
} else {
var $divId = "window" + i;
$('<div class="window" id="window' + i + '" />')
.append('<div class="topright" id="topright' + i + '"/>')
.append('<div class="topleft" id="topleft' + i + '"/>')
.append('<div class="bottomright" id="bottomright' + i + '"/>')
.append('<div class="bottomleft" id="bottomleft' + i + '"/>')
.append($('<p id="to' + i + '">').text(getTo.text()))
.appendTo('div#demo1');
var countr = $(this).find('COUNTRY').text();
$('div#topright' + i).append(countr);
$('div#topleft' + i).append('NW');
$('div#bottomleft' + i).append('SW');
$('div#bottomright' + i).append('SE');
}
arr[i] = getTo;
}); //end of find
}
}); //end of ajax function
var j;
alert("3");
谢谢!
最佳答案
请注意,Google Chrome 浏览器无法通过 XmlHttpRequest(用于 ajax 和 jQuery.ajax
函数)处理本地文件。因此,您无法加载路径类似于 file://...
的文件。
关于javascript - .ajax 调用在 Chrome 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12118813/