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.

1
  • 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. Commented Nov 8, 2012 at 10:25

2 Answers 2

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">
Sign up to request clarification or add additional context in comments.

1 Comment

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 :)
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.