Archive for July, 2007

AJAX Response Text and Question Marks (Replacement Characters)

Monday, July 2nd, 2007

So in my Firefox plugin development phase, I’ve been working with a lot of AJAX to enable new “Web 2.0″ functionality in some sites that I go to. Recently one of my testers encountered a bug with this AJAX that caused non-ASCII characters (e.g. ») returned by the responseText property of the XMLHttpRequest object to turn into replacement characters.

This has to do with the MIME encoding used by the server to return the response you’re looking for. This is encapsulated in the Content-Type response header. If that header doesn’t contain the correct information to actually parse the response, then the XMLHttpRequest object will return these “replacement characters” (\uFFFD) to substitute the characters it doesn’t recognize in its responseText.

One way to override this Content-Type so that the responseText will have the “expected” text is to use the overrideMimeType method on the object, which, as its name suggests, “overrides the mime type returned by the server (if any). This may be used, for example, to force a stream to be treated and parsed as text/xml, even if the server does not report it as such. This must be done before the send method is invoked.” (XULPlanet - http://www.xulplanet.com/references/objref/XMLHttpRequest.html#method_overrideMimeType)

For example:

// getXmlHttp() = browser-friendly way to create XMLHttpRequest

var xmlHttpRequest = getXmlHttp();
xmlHttpRequest.overrideMimeType(”application/xml; charset=ISO-8859-1″);

// Do other work normally

The syntax for the String input for the overrideMimeType method is the same as the Content-Type response header. Setting the content-type as application/xml will also allow you to use the responseXML property of the XMLHttpRequest even though the server might not report the page as XML. Hope this helps anyone that’s been experiencing these sorts of problems.