Login Register

dojo.xhrGet and local XML files in Mozilla

I am trying to use dojo.xhrGet to load an XML file when all my pages are loaded from the local filesystem (not on a server). The file contains valid XML but does not have an XML extension (let's say it's xml_data.dat).

Because it's a local file, no http headers are used to set the content/mime type as it looks like Mozilla (Firefox 2 and Seamonkey) is trying to guess the mime type by file extension. That fails and I only get a responseText in the xhr.

I got around it by using XMLHttpRequest.overrideMimeType() method available in Mozilla browsers.
I override the setRequestHeader method of the XMLHttpRequest to add the overrideMimeType() call.

function patchSetRequestHeader(header, value) {
	this._setRequestHeader(header, value)
	try {
		if (header == "Content-Type" && value.match(/^[^;\s]+/) )  {
			this.overrideMimeType(value.match(/^[^;\s]+/)[0]);
		}
	} catch(e) {}
}

try{
    XMLHttpRequest.prototype._setRequestHeader = XMLHttpRequest.prototype.setRequestHeader;
    XMLHttpRequest.prototype.setRequestHeader = patchSetRequestHeader;
} catch(e) {}

It's really not a good idea, much better would be to do it in dojo.xhrGet code (e.g. _doIt method) if handleAs is set to XML.

Any suggestions?