$(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 上运行不佳。有什么问题吗?
最佳答案
默认情况下,在本地、chrome 和 IE 上不支持 ajax 请求。 (同源错误)。
请尝试将您的文件直接放在服务器上。或者在您的本地服务器上激活足够的 header (Access-Control-Allow-Origin)。
并添加content-type和发送请求参数:
$.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.'
关于javascript - JQuery 不适用于 Chrome 和 IE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36687998/