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.
Tags: ajax, ie7, microsoft, Web Development






April 13th, 2007 at 1:25 pm
Hi!
You could use request.setRequestHeader(”Content-Type”, “text/xml”); instead I think.
June 15th, 2007 at 12:05 pm
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?
March 3rd, 2008 at 8:12 pm
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
March 3rd, 2008 at 8:24 pm
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.
April 30th, 2008 at 6:25 pm
Thanks that helped me out