[英]JQuery not working with Chrome and IE
$(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請求。 (同源錯誤)。
請嘗試將您的文件直接放在服務器上。 或者在本地服務器上有足夠的標頭(Access-Control-Allow-Origin)。
並添加content-type和send request參數:
$.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錯誤'否'Access-Control-Allow-Origin'標頭出現在請求的資源上。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.