Spell “UTF-8” correctly or IE and jQuery will cry

Today’s lesson: When setting character encoding to UTF-8 for your web site, be sure to specify character encoding for ajax calls, and make sure the character set is spelled “UTF-8”. If you don’t, jQuery’s various AJAX features may fail on Internet Explorer (IE).

Working on a health-monitoring web site, we ran into a problem with a jQuery autocomplete widget. It worked fine on Firefox, but failed on IE (8 and 7). We checked the network traffic using Fiddler … the server was returning the data correctly. But the jQuery callback didn’t seem to be running.

We noticed on another page that a box of data wasn’t prepopulating – again, it was failing only on IE. That page uses the jQuery.post() method to prepopulate a list of exercise activities. Again, Fiddler confirmed that the server was returning the data, but the jQuery callback wasn’t running.

We grabbed this line of code from the jQuery ajaxError comments section to see what the actual error was…

$(document).ajaxError(function(e, xhr, settings, exception) {
   alert('error in: ' + settings.url + ' \\n'+'error:\\n' + exception);
 });

It wasn’t very helpful, but we did get an error code to search on — “c0056e”. That pointed us to this thread on the tizag web site. It indicated we had found a very nit-picky problem on IE having to do with character encoding. We had recently started enforcing UTF-8 encoding on the entire web site, so this seemed a likely culprit.

Sure enough, we looked in our php.ini file and discovered that our default_charset setting was “UTF8” while the commenter on tizag said it had to be “UTF-8” (with a dash). We added the dash and immediately the jQuery preload and the autocomplete began working again in IE8.

Advertisements
This entry was posted in Web programming. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s