JQuery.html method character encoding

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

I made three simple files to show the problem:

PHP file:

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

JavaScript file (I am using 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 Prueba link, it shows:

 Prueba n  uno dos         

The current site works fine, but it does not use ajax, and it is on the same server where I do it, so how can I tell jquery to return ISO-8859-1 instead of what it returns? I know that the ideal is to always use utf-8, but changing it to utf-8 will give us some problems that we cannot afford right now.

+4
source share
2 answers

In order for the browser to use the correct encoding, you need to add an HTTP header to the php page:

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

or you can put the encoding in the html meta tag:

 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> 
+4
source

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

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

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

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

And for good practice, specify the type of document:

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

<!DOCTYPE html> is a legitimate HTML5 doctype .


And as Pranav Kapoor pointed out, you will also need to specify the encoding of the PHP files:

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

I see that you have specified your encoding: ISO-8859-1 . I always work with UTF-8 . You can learn more about this here: What is the difference between UTF-8 and ISO-8859-1?

But remove contentType from your ajax call

0
source

Source: https://habr.com/ru/post/1444765/


All Articles