【问题标题】:jQuery XML Parse (with Ajax) Does Not Work in Chrome (but works in IE, FF, Opera, Safari)jQuery XML Parse(使用 Ajax)在 Chrome 中不起作用(但在 IE、FF、Opera、Safari 中起作用)
【发布时间】:2010-09-13 14:19:49
【问题描述】:

我正在使用 jQuery.ajax 从文件中解析一些 xml。在 IE (6,7,8)、Firefox、Opera 和 Safari 中一切正常,但在 Google Chrome 中失败。代码如下:

/* ... */
this.loadXml = function()
{
 $.ajax(
 {
  type: "GET",
  url: "some_file.xml",
  dataType: ($.browser.msie) ? "text" : "xml",
  success: function(xml)
  {
   if($.browser.msie)
   {
    var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.loadXML(xml);
    xml = xmlDoc;
   }

  /* parsing starts here */
  /* for example: in the document I have a div tag with id "some text" and the xml file contains: <root><tag>test</tag></root>*/

  $("#some_id").text($(xml).find("root > tag").text());

  /* parsing ends here */

  }
 });
}

【问题讨论】:

    标签: jquery xml google-chrome


    【解决方案1】:

    Chrome 不会让您访问这样的本地文件。这是一个(有点争议的)安全问题。您可以从带有标志的命令行启动 chrome 以强制它允许访问:

    google-chrome --allow-file-access-from-files
    

    (再次感谢@Nick Craver,知识渊博的人。)

    编辑——这是我问的问题:Accessing relative URL's via "ajax" from "file://" content

    【讨论】:

    • 是的...这似乎是问题...感谢您的快速回复!
    【解决方案2】:

    只需将短网址替换为完整网址...

    来自

    网址:“some_file.xml”,

    网址:“http://www.domain.com/some_file.xml”,

    【讨论】:

    • 加载本地 .xml 文件时它是如何工作的? (这就是问题的根源)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-20
    • 2012-08-19
    • 2013-03-12
    • 1970-01-01
    • 1970-01-01
    • 2013-01-04
    • 1970-01-01
    相关资源
    最近更新 更多