本地 html 文件 AJAX 调用和 jQuery 问题 [英] Local html file AJAX Call and jQuery Woes

查看:36
本文介绍了本地 html 文件 AJAX 调用和 jQuery 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

当我查看错误时,我可以告诉它正在加载 XML 文件,因为它在错误的 responceText 属性中.它在 Firefox 中运行良好.

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.

这就是我的电话的样子

$.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.

关于如何在 IE 中实现此功能的任何想法?

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

我找到了问题的答案.这里

推荐答案

来自 linkOP 发布了答案:

From the link that the OP posted with the answer:

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

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

 $.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"
   }
 }); 

这对我也有效.

这篇关于本地 html 文件 AJAX 调用和 jQuery 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆