AJAX Response Text and Question Marks (Replacement Characters)
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.
Syndication
July 26th, 2007 at 3:06 pm
I can honestly say that I wouldn’t have imagined that happening.
An interesting find though, I must say.
May 25th, 2008 at 10:38 pm
Thank you so much!!! I tried anything to solve this problem!!
July 17th, 2008 at 11:51 pm
[...] Before going again into the structure of .vsct files I would like to turn your attention to David DeWinter’s blog. David wrote two great articles on creating dynamic menus in VS. You can find them here: Part #1 and Part #2. [...]