admin管理员组

文章数量:1318573

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.

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.

Share Improve this question edited May 23, 2017 at 12:08 CommunityBot 11 silver badge asked Nov 8, 2012 at 10:17 NewRehtseNewRehtse 3382 gold badges5 silver badges15 bronze badges 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. – NewRehtse Commented Nov 8, 2012 at 10:25
Add a ment  | 

2 Answers 2

Reset to default 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">

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

本文标签: javascriptJQuery html method character encodingStack Overflow