【问题标题】:JQuery not working with Chrome and IEJQuery 不适用于 Chrome 和 IE
【发布时间】:2016-08-09 20:06:38
【问题描述】:
$(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 上无法运行。它有什么问题?

【问题讨论】:

  • 我收到“无法读取文件”的错误
  • 按 F12 获取错误日志并检查
  • 它会帮助您告诉我们什么不起作用。你看到了什么,你期望看到什么。

标签: javascript jquery google-chrome


【解决方案1】:

默认情况下,在本地、chrome 和 IE 上不支持 ajax 请求。 (同源错误)。

请尝试将您的文件直接放在服务器上。或在本地服务器上激活足够的标头(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.'

【讨论】:

    猜你喜欢
    • 2013-04-22
    • 1970-01-01
    • 2014-05-17
    • 1970-01-01
    • 2014-07-23
    • 1970-01-01
    • 1970-01-01
    • 2012-05-27
    • 1970-01-01
    相关资源
    最近更新 更多