Non-Ascii charactors in my AJAX responses !? Tags:, , , , , , , , , , , ,

A while ago, I co-wrote a large site with Ryan while I was working at Zipline Interactive, this site uses Mootools 0.98 as we found to be getting a lot of �������� all through the JAX requested response code.

For the life of US, we couldn’t figure out why? it’s not in the output if we called the data via a normal http request, so I looked for a fix.

  1. Upgrade the Mootools to version 1.2.2
  2. Add an enc type to the AJAX functions
    	new Request.HTML({
    		method: 'get',
    		url: '/examples/ajax.php',
    		data: { 'example': 'ajax-ascii' },
    		encoding: 'iso-8859-1',
    		onRequest: function() {
    			$('calendarMainEvent').empty().removeClass('calendarClosed');
    			$('calendarMainEvent').set('html', '<img src="images/whiteLoading.gif" alt="Loading" style=\"background: #FFFFFF; padding: 20px 200px 20px 275px;\" />');
    		},
    		onComplete: function(responseTree, responseElements, responseHTML, responseJavaScript) {
    			$('calendarMainEvent').empty().set('html', responseHTML);
    			$('calendarMainEvent').setProperty('class','calendarOpen');
    		},
    		onFailure: function() {
    			$('calendarMainEvent').empty().removeClass('calendarClosed');
    		}
    	}).send();
  3. Specifying the charset in the PHP that is outputting the AJAX response!

    < ?php
     
    if($_GET['ajax_request']) {
     
    header('Content-Type: text/html; charset=iso-8859-1');
    echo('<h3>Success');
    /* More output code */
     
    }

Matching the Character set

It didn’t occur to me right away that I need to specify the charset more than just in the <head> section of the page, but that was all it took.

Of course you want to match your PHP header, Javascript encoding and xHTML charsets all to the same with it is iso-xxx or utf-xx

Related posts:

  1. Using Mootools Request.HTML response Javascript using $exec
  2. Controlling Input with Mootools Javascript
  3. Alternating row styles with Javascript
  4. Javascript URI Address Segment Selection
  5. Clean Dynamic Directory Listing with PHP

Subscribe Via Email Notify me of followup comments via e-mail