jQuery, AJAX and Internet Explorer

The issue:
Internet Explorer won’t parse XML quite as easily as every other browser on the internet (I assume). You have to change what you pass it as through jQuery. In IE it has to be passed as text, while the rest can handle it as XML. You also have to pass it through a separate function that brings in ActiveX before you can navigate your way through the XML and use it in your application.

The solution:
Set the type to “text” for IE and “xml” for the rest.

<code>$.ajax({
    type: 'GET',
    url: 'sample.xml', // your xml file
    dataType: ($.browser.msie) ? &quot;text&quot; : &quot;xml&quot;, // text for IE, xml for the rest
    success: function(xml) {
        var newXML = parseXml(xml);
        // and then process normally
    }, // end success
    error: function(a, b, c) {
      alert('error'); // this is where the errors happen, 'b' and 'c' are typically the only ones with values
    } // end error
  }); // end .ajax

function parseXml(xml) {
  if (jQuery.browser.msie) {
    var xmlDoc = new ActiveXObject(&quot;Microsoft.XMLDOM&quot;);
    xmlDoc.loadXML(xml);
    xml = xmlDoc;
  }
  return xml;
}</code><a href="http://codepen.io/jeremyHixon/pen/HjFKk">Check out this Pen!</a>


Reference the function in your .ajax()

success: function(xml) {
  var newXML = parseXml(xml);
  $(newXML).find()....

And…voila!

32 Comments on “jQuery, AJAX and Internet Explorer”

  1. [...] You can get the function and instructions for IE here: jQuery, AJAX and Internet Explorer [...]

  2. imac says:

    This issue was driving me crazy for hours! Thanks a lot for the solution. I was examing my code bit by bit, I even came to become suspicious about my RSS generator, until I found your blog and voilá! Problem solved. I guess jQuery isn’t so cross-browser after all. ;)

    Thanks again.

  3. eggsy84 says:

    Thank you for this!!!

  4. rajhees says:

    i have used the same and it doesnt work for me i couldnt guess why can any one help

  5. cvaldex says:

    Many thanks…!!!

    Muchas gracias desde Chile… me salvaste una tarde de esfuerzo!

  6. [...] I don’t know how IE handles the “HTML” dataType but I know it struggles with XML. If this causes problems for you in IE, you I might suggest reading this topic: jQuery, AJAX and Internet Explorer [...]

  7. Alin Balan says:

    thanks, i had the same problem, this was very helpful

  8. Reaganama says:

    This was kicking my ass today. Nice solution, thanks!

  9. david says:

    Thankyou so much!!

  10. Yamen Al-Haj says:

    Hello , thanks for the code but i still cant read from IE .. can you help me please

    here is the Code :

    function parseXml(xml) {
    if (jQuery.browser.msie) {
    var xmlDoc = new ActiveXObject(“Microsoft.XMLDOM”);
    xmlDoc.loadXML(xml);
    xml = xmlDoc;

    }
    return xml;
    }
    $.ajax({
    type: “GET”,
    url: myXML path, //xml file
    dataType: ($.browser.msie) ? “text” : “xml” ,
    success: function(xml) {
    var newXML = parseXml(xml);
    var count = 0; //counter
    $(newXML).find(‘item’).each(function() {
    var title = $(this).find(‘title’).text();
    var url = $(this).find(‘guid’).text(); //url
    var imageURL = $(this).find(‘enclosure’).attr(“url”); //location of the image
    var desc = $(this).find(‘description’).text(); //alternate text of the image
    images[parseInt(count)] = new Array(imageURL);
    titles[parseInt(count)] = new Array(title);
    urls[parseInt(count)] = new Array(url);
    descs[parseInt(count)] = new Array(desc);
    count++;
    });

  11. Jeremy says:

    @Yamen Al-Haj:
    First of all, is it working in Firefox and not IE or not working in either?

    Second, a question. did you check the filename that goes in the “url” of the ajax call?

    Third, I can’t find where the closing bracket is for your “success” function. Looking at your code I’d think it would be just before the closing of your .ajax function. Something like this, would be the last two lines of your code:

    }
    });

  12. Wes says:

    Nice workaround. Why can’t IE be like the rest of them??

  13. Ali Imraan says:

    Thanks a lot. I googled for 6 hrs and finally got the solution. Nice Work Buddy.

  14. Jquery says:

    Thanks buddy…solution is working fine in IE….great job.

  15. Ray says:

    Yeah, baby! This example helped put that little brat IE in his place. From now on, it will behave like its peers.

  16. Oz says:

    Thanks for the tip, you saved the day!

  17. maruthi says:

    I m getting exception in this line

    xmlDoc.loadXML(xml);

    “Type Mismatch”

    • admin says:

      Check the “dataType” and make sure it matches the type of data you’re requesting. A quick Google looks like most of the time that happens when you try to pull in a JSON response through the XML parser or if the XML document doesn’t have a root element.

  18. Saleck says:

    J’ai le même problème mon code fonctionne bien sur Firefox et google chrome mais pas sur IE 7.
    Pourriez vous m’aider svp.
    merci par avance

    $.ajax({
    url : url,
    dataType : ‘json’,
    ‘cache’ : ‘false’,
    jsonp : ‘jsoncallback’,
    success : function(data){processEmailPhoneData(data)},
    error:config.onError

    });

    • Jeremy says:

      Not sure if the JSON method is the same as the XML method but if that’s the case then you’re going to need to use ‘text’ for IE and ‘json’ for everything else like so

      $.ajax({
      url : url,
      dataType : ($.browser.msie) ? 'text' : 'json',
      cache : 'false',
      jsonp : 'jsoncallback',
      success : function(data){processEmailPhoneData(data)},
      error:config.onError
      });

      Not sure if IE requires an extra step to convert the JSON response to something it can read or not. For example, in my example you have to create a new ActiveXObject("Microsoft.XMLDOM") in order to be able to parse it.

      • Saleck says:

        Désolé pour ce silence,
        J’ai fais ce que tu m’a demandé de faire Jeremy mais pas de changement sous ie.
        pourriez m’aide.
        merci par avance

  19. Saleck says:

    au lieu d’afficher la fonction il affiche toujours la fonction erreur sous ie.

  20. Anshu says:
    $.support.cors = true;
            $.ajax({
                type: 'GET',
                url: url,
                cache: true,
                async: true,
                crossDomain: true,
                dataType: 'text',
                contentType: 'application/javascript',
                success: function (data, status) {
                    alert(data.firstName);
                },
                error: function (xhr, textStatus, errorThrown) {
                    alert(errorThrown);
    
                }
            });

    Need solution for IE 8/9 only :-
    If i used datatype is jsonp, then i got an eror “SCRIPT1004: Expected ‘;’ ” in {"id":1,"firstName":"Robert","lastName":"Plant","dob":"1948-08-20"} data and error thrown was jquery3432234234_34234242 was not called. I also added jsonpcallback params but not worked. While i used json or html or text, it gives an error “Access is denied”. How can i get an data from server for IE 8/9??

  21. Sumit says:

    Current jQuery version doesn’t support $.browser and have a parseXML function allready available. What’s the fix now?!

    • Jeremy says:

      That’s a good question. I haven’t been keeping up with the updates to jQuery’s ajax method. I’ll take a look as soon as I can and try to post an update.

  22. stoshi says:

    Thanks a lot,
    Now it works on IE8 and Firefix, but can’t understand why it doesn’t work on Chrome anymore…
    Love web dev

  23. stoshi says:

    Thanks a lot,
    Now it works on IE8 and Firefox, but can’t understand why it doesn’t work on Chrome anymore…
    Love web dev

  24. Raja Ghosh says:

    presently I am working on the code

    as per the article i have tried my code
    but it is of no use same issue with IE

    function show_code_update_table(str)
    {
    var xmlhttp1;
    if (str==”")
    {
    document.getElementById(“txtHint”).innerHTML=”";
    return;
    }
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp1=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
    //xmlhttp1=parseXml(xmlhttp1);
    xmlhttp1=new ActiveXObject(“Microsoft.XMLDOM”);
    //xmlhttp1=new ActiveXObject(“Microsoft.XMLHTTP”);
    }
    xmlhttp1.onreadystatechange=function()
    {
    if (xmlhttp1.readyState==4 && xmlhttp1.status==200)
    {
    document.getElementById(“final_form_for_update”).innerHTML=xmlhttp1.responseText;
    }
    }

    var sr_no_data = $(“#sr_no_data_final”).val();
    var code_data = $(“#code_data_final”).val();
    var description_data = $(“#description_data_final”).val();
    var table_name=”code_area”;

    xmlhttp1.open(“POST”,”table_code_update.jsp?sr_no_data=”+ sr_no_data + “&code_data=” + code_data + “&description_data=” + description_data + “&table_name=” + table_name ,true); //here str means the serial no.
    xmlhttp1.send();

    var x1;
    x1=xmlhttp1;
    showtable(xmlhttp1.value);
    showtable(x1.value);

    }

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>