how to read xml data in IE browser using jquery Ajax function

前端 未结 4 1529
悲哀的现实
悲哀的现实 2021-01-24 21:06

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 t

相关标签:
4条回答
  • 2021-01-24 21:37

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

    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!!

    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;
    }
    
    0 讨论(0)
  • 2021-01-24 21:40
     function parseXML(xml) {
        if (jQuery.browser.msie) {
            alert("dd3");
            var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
            xmlDoc.loadXML(xml);
            xml = xmlDoc;
        }
        alert("dd4");
        return xml;
    }
    
    function searchThis() {
        alert("dd1");
        $.ajax({
            type: "GET",
            url: XMLSource,
            dataType: ($.browser.msie) ? "text" : "xml",
            success: function (xml) {
                alert("dd2");
                var newXML = parseXML(xml);
                loadPublication(newXML)
            }
        });
    }
    

    enjoy with this working for IE.

    0 讨论(0)
  • 2021-01-24 21:40

    You no need to repeat the xml file name and do the same operations again in parseXML unnecessarily.

    The trick here is to disable the caching. But still IE sometimes doesn't disable caching. Hence add the timestamp as a query string along with your xml file name in the url which solves the problem. I tested it and worked 100% on IE and other browsers.

    $.ajax({
      type: 'GET',
      url: "XML_file.xml?timestamp=" + new Date().getTime(),   // add the timestamp to the url to avoid caching in IE
      dataType: ($.browser.msie) ? "text" : "xml",
      cache: "false",
      success: function (xml) {
    
         var processedXML = parseXml(xml);
    
         $(processedXML).find('my record').each(function () {  //code  } 
      }
    });
    
    function parseXml(xml) {
       if (jQuery.browser.msie) {    // Only for IE
          var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
          xmlDoc.async="false";
          xmlDoc.loadXML(xml);
          xml = xmlDoc;
       }
       return xml;
    }
    
    0 讨论(0)
  • 2021-01-24 21:52

    i manage problem with chrome by adding chrome.exe --allow-file-access-from-files to its property target location but not getting for IE. please any one can then post your answer.

    0 讨论(0)
提交回复
热议问题