如何使用jquery Ajax函数在IE浏览器中读取xml数据 [英] how to read xml data in IE browser using jquery Ajax function

查看:47
本文介绍了如何使用jquery Ajax函数在IE浏览器中读取xml数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在AJAX函数中使用jquery读取xml数据,在Firefox中可以正常工作...但是我被IE浏览器卡住了...我无法读取xml.程序需要从xml文件读取"proptype".我有地方警报Alert(theXml),但是在IE中却没有给我答案,但是它在Firefox浏览器中可以正常工作

I need to read xml data using jquery in AJAX function, which is working fine in firefox... however i am stuck with IE browser... I am not able to read xml. program is required to read "proptype" from xml file. I have place alert alert(theXml) but is not giving me answer in IE however it is working in firefox browser

这是我的代码.

<!DOCTYPE html>
<html>
<head>
<title></title>

 <script src="../scripts/jquery-1.9.1.min.js"></script>
 <script src="../scripts/jquery-migrate-1.2.1.min.js"></script>

 <script>

    $(document).ready(function () {

        testXml();

    });


    function testXml() {

        $.ajax({
            type: 'GET',
            url: 'XML_estatesIT_op4.xml',
            dataType: ($.browser.msie) ? "text" : "xml",
            success: function (xml) {

                theXml = parseXml(xml);

                alert(theXml);

                $(theXml).find("property").each(function () {

                    var b1 = $(this).find('proptype').text();

                    alert(b1);                        
                });
            },
            error: function () {
                alert("An error occurred while processing XML file.");
            }
         });
    }

   function parseXml(xml) {

        if (jQuery.browser.msie) {
            var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
            xmlDoc.async = false;
            xmlDoc.loadXML(xml);
            xml = xmlDoc;
        }

        return xml;
    }

   </script>
 </head>
 <body>

 </body>
</html>

===== XML =====

=====XML=====

<properties>
   <property>
  <propcode>DEMO1_000001</propcode>
  <address6>Cambridgeshire</address6>
  <postcode>PE28 2BG</postcode>
  <ccode>UK</ccode>
  <priceask>360,000</priceask>
  </property>
</properties>

推荐答案

最后,我找到了解决方案,诀窍是为版本小于10的IE浏览器使用单独的代码XML.

finally I have found the solution, the trick is use separate code XML for IE browsers that are version of less than 10 .

因此,每次调用Ajax时,将使用输入参数XML Dom或文本来调用parseXml方法,具体取决于浏览器....如果当前浏览器是IE,则它将上载XML doc,并根据Microsoft标准进行处理并返回XML Ajax中的其余过程将按预期进行!!

so every time Ajax is call a method parseXml is called with input parameter XML Dom or text, depending on browser .... and if current browser is IE, it upload XML doc, process it according to Microsoft standards and return XML and rest of processes in Ajax carries on as expected!!

注意:jQuery 1.9不支持browser.msie,但是您可以添加jquery-migrate-1.2.1.min.js使其兼容或使用userAgent来查找当前浏览器

note : browser.msie is not supported in jQuery 1.9 but you can add jquery-migrate-1.2.1.min.js in order to make it compatible or use userAgent and find which is current browser

$.ajax({
  type: 'GET',
  url: 'XML_file.xml',
  dataType: ($.browser.msie) ? "text" : "xml",
  success: function (xml) {

     var processedXML = parseXml(xml);

     $(processedXML).find('my record').each(function () {  //code  } 
  });


 function parseXml(xml) {

  if ($.browser.msie)  {

    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp = new XMLHttpRequest();
   }
   else {// code for IE6, IE5
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
   }

    xmlhttp.open("GET", "XML_file.xml", false);
    xmlhttp.send();
    xmlDoc = xmlhttp.responseXML;

    xml = xmlDoc;
  }
        return xml;
}

这篇关于如何使用jquery Ajax函数在IE浏览器中读取xml数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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