$(document).ready(function(){ // load jQuery 1.5
function loadfail(){
alert("Error: Failed to read file!");
}
function parse(document){
$(document).find("el").each(function(){
var optionLabel = $(this).find('text').text();
var optionValue = $(this).find('value').text();
$('#el').append(
'<option value="'+ optionValue + '">' + optionLabel + '</option>'
);
});
}
$.ajax({
url: "ednlevel.xml", // name of file with our data - link has been renamed
dataType: 'xml', // type of file we will be reading
success: parse, // name of function to call when done reading file
error: loadfail // name of function to call when failed to read
});
});
以上代码适用于Firefox,但它不适用于chrome或Internet Explorer。这有什么问题?
答案 0 :(得分:0)
默认情况下,本地,Chrome和IE不使用ajax请求。 (同源错误)。
请尝试将您的文件直接放在服务器上。或者在本地服务器上有足够的标题(Access-Control-Allow-Origin)。
并添加内容类型和发送请求参数:
$.ajax({
type:"get",
url: "ednlevel.xml", // name of file with our data - link has been renamed
dataType: 'xml', // type of file we will be reading
contentType: "text/xml",
success: parse, // name of function to call when done reading file
error: loadfail // name of function to call when failed to read
});
您可以在此处找到更多信息:jQuery xml error ' No 'Access-Control-Allow-Origin' header is present on the requested resource.'