Friday 29 July 2011

Fixing UTF encoding in AJAX requests on Internet Explorer

The legendary status of Internet Explorer for being special among all browsers was reminded to me when I discovered that sending a variable with non-latin character in a GET request over AJAX is apparently too troubling for IE. The result is that characters in greek are either transformed to latin ones or just disappear. This was all in a simple call like this:
[code lang="js"]$("#search_result").load("/ajaxsearch/&id=$id&f="+what);[/code]
(Where [i]what[/i] was the value of an input used in a AJAX-powered "quick search" form. Calling the same URL on a normal (non-AJAX) request everything worked perfectly.
The solution (after trying for hours to figure out what kind of transformation IE was doing on the value of the string), was simple enough: send it via POST
[code lang="js"]
$.post("/ajaxsearch/&id=$id&",{ f: what },
function(returned_data) {
$("#search_result").html(returned_data);
});[/code]
I guess since it was a input box, I should have treated the call as a form and used POST from the start, right?

No comments:

Post a Comment