javascript - JQuery 不适用于 Chrome 和 IE

标签 javascript jquery google-chrome

$(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/

相关文章:

javascript - JS : Associative array access over variable?

javascript - 浏览器嗅探用户的历史 - 就此结束?

javascript - Alfresco使用CMIS获取文档路径

javascript - Node : what is require modules best practices?

jQuery:语法错误?当我试图让我的代码变得更好时,意外的 token 非法。 (带有回车符和制表符等)

jquery - 带调整大小预览的 Jcrop

c# - 根据列值显示/隐藏 jQuery 数据表操作链接按钮

html - 新 Ghost 博客上的链接仅在 Chrome 中不起作用的问题

linux - Linux 上的 NPAPI 和 Google Chrome

ajax - "Cache-Control: only-if-cached"离线时即使响应被缓存也会失败