本地 html 文件 AJAX 调用和 jQuery 问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/436670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 09:08:39  来源:igfitidea点击:

Local html file AJAX Call and jQuery Woes

jqueryxmlajaxinternet-exploreroffline

提问by Superdumbell

I'm working on a offline version of a website using jQuery and some xml files. I'm running in to a problem in jQuery when I do a $.ajax call on a xml file jQuery throws a error.

我正在使用 jQuery 和一些 xml 文件处理网站的离线版本。当我对 xml 文件执行 $.ajax 调用时,我在 jQuery 中遇到了问题 jQuery 抛出错误。

When I look at the error I can tell its loading the XML file because its in the error's responceText property. It seams to work just fine in Firefox.

当我查看错误时,我可以告诉它正在加载 XML 文件,因为它在错误的 responceText 属性中。它在 Firefox 中工作得很好。

This is how my call looks

这是我的电话的样子

$.ajax({
    type: "GET",
    url: "Modules/" + ModuleID + "/ModuleContent.xml",
    dataType: "xml",
    success: function(x) { xml = x; ProcessXML(); },
    error: function(x) { alert(x.responceText); }
});

When I run this on a web server it works just fine. Its only when I run it from the file its self when I have this problem.

当我在 Web 服务器上运行它时,它工作得很好。只有当我遇到这个问题时才从文件本身运行它。

Any ideas on how I can make this work in IE?

关于如何在 IE 中完成这项工作的任何想法?

Edit: I found the answer to my problem. Here

编辑:我找到了我的问题的答案。这里

采纳答案by Mark A. Nicolosi

From the linkthat the OP posted with the answer:

从OP 与答案一起发布的链接

When loading XML files locally, e.g. a CD-ROM etc., the data received by Internet Explorer is plain-text, not text/xml. In this case, use the dataType parameter to load the xml file as text, and parse the returned data within the succes function

在本地加载 XML 文件时,例如 CD-ROM 等,Internet Explorer 接收到的数据是纯文本,而不是 text/xml。在这种情况下,使用 dataType 参数将 xml 文件加载为文本,并在 succes 函数内解析返回的数据

 $.ajax({
   url: "data.xml",
   dataType: ($.browser.msie) ? "text" : "xml",
   success: function(data){
     var xml;
     if (typeof data == "string") {
       xml = new ActiveXObject("Microsoft.XMLDOM");
       xml.async = false;
       xml.loadXML(data);
     } else {
       xml = data;
     }
     // Returned data available in object "xml"
   }
 }); 

This worked for me as well.

这对我也有效。

回答by ohnoes

Just a thought: I remember some GET requests failures with IE. Have you tried POSTing it?

只是一个想法:我记得 IE 的一些 GET 请求失败。你试过发布它吗?