xmlHttpReq.overrideMimeType() in IE7

This is just a little note for anyone doing xmlHttp work. I just encountered this situation this morning. As most web developers know IE7 introduces support for the native scriptable XMLHttpRequest object. The big advantage here is that ActiveX is no longer necessary to use ajax applications on IE. One thing I did note is that there is a slight difference in their support for the XMLHttpRequest object. Take the following code:

// Mozilla/Safari/IE7+
if (window.XMLHttpRequest) {
    xmlHttpReq = new XMLHttpRequest();
    xmlHttpReq.overrideMimeType(‘text/xml’);
}
// IE6-
else if (window.ActiveXObject) {
    xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlHttpReq.open(‘POST’, strURL, true);

That seems to break for me on IE7. A little experimentation shows that it doesn’t support the overrideMimeType() method. A simple fix for this is to simply check before invoking it as follows:

// Mozilla/Safari/IE7+
if (window.XMLHttpRequest) {
    xmlHttpReq = new XMLHttpRequest();
    if(xmlHttpReq.overrideMimeType){       
        xmlHttpReq.overrideMimeType(‘text/xml’);
    }
}
// IE6-
else if (window.ActiveXObject) {
    xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlHttpReq.open(‘POST’, strURL, true);

This is just FYI for anyone who happens to encounter this error. It’s a simple fix. This somewhat goes without saying, but make sure your request returns from the server as text/xml or you’ll likely still encounter issues.

9 thoughts on “xmlHttpReq.overrideMimeType() in IE7

  1. Hi!
    You could use request.setRequestHeader(“Content-Type”, “text/xml”); instead I think.

  2. I am encountering an issue but it seems to be reverse of what you encountered. I found that you could invoke overrideMimeType in IE7 but i used it after the send call, to ensure that the data returned was in XML format. However, this same code didnt function at all in IE6. I get an Object required and method not supported for this Object type. The following works in IE7 and Firefox 1.5, but not in IE6. Any thoughts as to why?

    function changeCardMessage(messageID) {
     
        var url="getMessageDetails.asp";
        request.open("POST", url, true);
        request.onreadystatechange = changeMessage;
        request.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); //required to post the data to the URL
        request.send("templateID=" messageID);
     
        if(request.overrideMimeType){                
           request.overrideMimeType("text/xml");
        }
    }
  3. Hi !

    My problem is very simple. The code is :

    if(req.overrideMimeType)
    req.overrideMimeType(‘text/html; charset=iso-8859-1’)

    It’s OK with Mozillas. IE gives :
    le 11 d�cembre 2007, � 19 heures 15, vous avez proc�d� �

    How would you do ? Thanx.

    Sacapuss

  4. Here is a better way to present the problem :

    Hi !

    Here is what it gives :
    le 11 d�cembre 2007, � 19 heures 15, vous avez proc�d� �

    I put the code :
    if(req.overrideMimeType)
    req.overrideMimeType(’text/html; charset=iso-8859-1′)

    it works fine with Mozillas. IE sends an error.

    How would you do ? Thanx.

  5. @WebMonseter

    You can not use setRequestHeader.
    That sets the request header in the request itself.

    overrideMimeType overrides the mime type sent back from the server.

    The two are not interchangeable.

    Cheers.

  6. You can avoid calling xmlHttpReq.overrideMimeType by setting the content-type in the XML response it self.

    For example if the XML is generated by a PHP script you can call the following at the beginning of the PHP:
    header(“Content-type: text/xml”);

    In JSP:
    %@page contentType=”text/html” %>

    Java Servlet:
    response.setContentType(“text/plain”);

    In ASP:
    response.ContentType=”text/xml”

  7. robert;

    i banged my head for over 2 weeks..
    tonight i read your article and you solved my problem.
    i just wanted to thank you.

    ciao from italy, pietro.

  8. Pingback: ajax中加一个判断的用处是? - web标准 - 开发者问答

Leave a Reply

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