AJAX Character Encoding in Internet Explorer
Wednesday, July 16th, 2008Is there no end to the woes of IE?
I’ve just spent 3 hours trying to find out why IE 7 won’t encode my form inputs in UTF-8 before sending them off in an XMLHttpRequest package to my server.
Here’s what is supposed to happen:
I send the page to the browser with UTF-8 character encoding, so the browser is supposed to use this encoding in whatever it does.
This is fine when sending form inputs back to the server with a straight-forward FORM Action routine, but when you use XMLHttpRequest in IE7, IE7 insists on sending the form input in ISO-8859-1.
So if someone enters Ø in one of my form inputs, IE7 sends this off to the server as ‘\xd8′ rather than as ‘%C3%98′ as it should.
The result is that garbage goes into my DB which then invalidates any XML that uses that garbage.
Thankfully, I found a solution.
When I take the form input value from my DOM, I pass it through the Javascript encodeURIComponent function before passing it to the XMLHttpRequest object.
var new_value = document.getElementById(”text_input”).value;
//CONVERT FORM INPUT TO UTF-8 FOR IE 7
new_value = encodeURIComponent(new_value);
XMLHttpRequest is supposed to use UTF-8 by default. There may be something wrong with my set up, but for the life of me I couldn’t get it to use UTF-8 (I’ve checked all my HTTP headers and everything is UTF-8; my server also uses UTF-8 by default, and everything works fine in FF) so this was the only solution that worked.
Another day lost to IE7.
Grrrrr.