Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery AJAX Character Encoding

I'm currently coding a French website. There's a schedule page, where a link on the side can be used to load another day's schedule.

Here's the JS I'm using to do this:

    <script type="text/javascript">     function load(y) {         $.get(y,function(d) {             $("#replace").html(d);             mod();         });     }     function mod() {         $("#dates a").click(function() {             y = $(this).attr("href");             load(y);             return false;         });     }     mod();     </script> 

The actual AJAX works like a charm. My problem lies with the response to the request.

Because it is a French website, there are many accented letters. I'm using the ISO-8859-15 charset for that very reason. However, in the response to my AJAX request, the accents are becoming ?'s because the character encoding seems to be changed back to UTF-8.

How do I avoid this? I've already tried adding some PHP at the top of the requested documents to set the character set:

<?php header('Content-Type: text/html; charset=ISO-8859-15'); ?> 

But that doesn't seem to work either. Any thoughts?

Also, while any of you are looking here...why does the rightmost column seem to become smaller when a new page is loaded, causing the table to distort and each <li> within the <td> to wrap to the next line?

Cheers

like image 956
Salty Avatar asked Feb 16 '09 14:02

Salty


People also ask

Is AJAX still used?

AJAX is still relevant and very popular, but how you write it may change based on what libraries or frameworks are in the project. I almost never use the "raw" JavaScript way of writing it because jQuery makes it easier and is almost always already loaded into a project I'm working on.

How does AJAX work in jQuery?

jQuery provides several methods for AJAX functionality. With the jQuery AJAX methods, you can request text, HTML, XML, or JSON from a remote server using both HTTP Get and HTTP Post - And you can load the external data directly into the selected HTML elements of your web page!

How can I get specific data from AJAX response?

You can't as it's asynchronous. If you want to do anything with it, you need to do it in a callback. How? Because it's asynchronous, javascript will fire off the ajax request, then immediately move on to execute the next bit of code, and will probably do so before the ajax response has been received.


1 Answers

Specifying the content type on the AJAX-call solved my problems on a Norwegian site.

$.ajax({         data: parameters,         type: "POST",         url: ajax_url,         timeout: 20000,         contentType: "application/x-www-form-urlencoded;charset=ISO-8859-15",         dataType: 'json',         success: callback }); 

You would also have to specify the charset on the server.

<?php header('Content-Type: text/html; charset=ISO-8859-15'); ?> 
like image 198
Magnar Avatar answered Sep 24 '22 19:09

Magnar