April 16, 2002
Using XMLHTTPRequest in Mozilla
Mozilla’s XMLHttpRequest object is in many ways identical to Microsoft’s XMLHTTP ActiveX object. Both can be used to make GET and POST requests from the browser. Here’s a handy tip: in Mozilla, don’t forget to use the little-known overrideMimeType() method if the server is returning anything other than text/xml. Otherwise, Mozilla might lock up on you.
try {
// for Mozilla
req = new XMLHttpRequest();
req.overrideMimeType(”text/xml”);
} catch (e) {
// for IE5+
req = new ActiveXObject(”Msxml2.XMLHTTP”);
}
req.open(”GET”, “page.html”,false);
req.send(null);
With overrideMimeType() Mozilla can fetch standard HTML pages, or just about anything the server would normally not return as text/xml. IE5+ doesn’t seem to have a problem with non-XML formats; probably due to its not-a-bug-it’s-a-feature behavior of ignoring mimetypes. The standard security restrictions still apply; you cannot do GET or POST requests to locations outside the current domain.










