4

I have a similar problem to this: jQuery AJAX Character Encoding but any solution mentioned there works for me.

I've made three easy files to show the problem:

PHP File:

//prueba.php
echo "nº one two € áéíóú";

JavaScript File (I use JQuery)

//Javascript file
    function prueba() {
        $.ajax({
            type: "GET",
            contentType: "application/x-www-form-urlencoded;charset=ISO-8859-1",
            url: "prueba.php",
        }).done(function( data ) {
            $("#prueba").text(data); 
            //$("#prueba").html(data); //It does the same encoding error 
        });
    }

** HTML File:**

<html>
    <head>
        <title>E-COMMERCE</title>
        <meta content="text/html; charset=iso-8859-1" http-equiv=Content-Type>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />

        <script src="javascript/jquery.js" type="text/javascript"></script>
        <script src="javascript/javascript.js" type="text/javascript"></script>
    </head>

    <body>
        <a href="javascript:prueba()">Prueba</a>
        <div id="prueba"></div>
    </body>

</html>

And when you click the link Prueba it shows:

Prueba
n� uno dos � �����

The current website works perfectly but it does not use ajax and it is in the same server where i am doing this, so How can I tell to jquery to return ISO-8859-1 instead of whatever it is returning? I know that the ideal is to use always utf-8 but changing to utf-8 it will give us some problems we cant afford right now.

Community
  • 1
  • 1
NewRehtse
  • 338
  • 1
  • 5
  • 15
  • It shows: "n? one two ? ????" but this example was really easy, I cannot do in the real one because it is returning a big string of html. – NewRehtse Nov 08 '12 at 10:25

2 Answers2

5

To make the browser use the correct encoding, you have to add an HTTP header to the php page :

header("Content-Type: text/plain; charset=ISO-8859-1");

or you could put the encoding in a meta tag of the html:

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
Pranav 웃
  • 8,469
  • 6
  • 38
  • 48
  • I solved the main problem in the original by putting both of them, the header in the php file that gives me the full html code, and in the html where I update the selector. Thank you very much! I was really stocked with... this :) – NewRehtse Nov 08 '12 at 10:36
0

In your php file you'll need to UTF-8 encode the output:

echo utf8_encode("nº one two € áéíóú");

And then in your html file you will need to set the charset:

<html>
<head>
    <meta charset="utf-8"/>
    ...

And for good practice specify a document type as well:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    ...

<!DOCTYPE html> is a legit HTML5 doctype.


And as Pranav Kapoor pointed out maybe you will need to specify the PHP-file charset aswell:

header("Content-Type: text/plain; charset=UTF-8");

I can see you have specified your charset to: ISO-8859-1. I always work with UTF-8 You can read more about it here: What is the difference between UTF-8 and ISO-8859-1?

But remove the contentType from your ajax call

Community
  • 1
  • 1
Andreas Louv
  • 46,145
  • 13
  • 104
  • 123