I’ve been trying to return special characters from an AJAX request to a PHP script.
The responding character string : abcde1’2’3’4’5“6”7–8é9é10’11’12’13ñ14ñ15’16ñ17ñ18 19 20é21é22í23ñ24ñ25’26ñ27ó28ú29’fghij
Using a JavaScript alert, it displays: abcde1â2â3â4â5â6â7â8é9é10â11â12â13ñ14ñ15â16ñ17ñ18 19 20é21é22Ã23ñ24ñ25â26ñ27ó28ú29âfghij
The code is below:
HTML File:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1"/>
<title></title>
<script src="js/phpTest.js" charset="ISO-8859-1" type="text/javascript"></script>
</head>
<body>
<input id="Submit1" type="submit" value="submit" onclick=" initXHR();return false;" />
</body>
</html>
JS File:
var xhr;
//;charset=ISO-8859-1
function initXHR() {
getXHRobj();
xhr.open('POST', 'phpinfo.php', true);
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.setRequestHeader("Content-length", 10);
xhr.setRequestHeader("Connection", "close");
xhr.send("");
return false;
}
function getXHRobj() {
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
}
else if (window.ActiveXObject) {
xhr = new ActiveXObject("Msxml2.XMLHTTP");
}
else {
throw new Error("Ajax is not supported by this browser");
}
xhr.onreadystatechange = function XHRresp() {
if (xhr.readyState == 4) {
if (xhr.status >= 200 && xhr.status < 300) {
document.write("1. " + xhr.responseText);
}
else {
alert(xhr.responseText);
}
}
}
}
And PHP file:
<?php
header("Content-Type: text/html;charset=ISO-8859-1");
print "abcde1’2’3’4’5“6”7–8é9é10’11’12’13ñ14ñ15’16ñ17ñ18 19 20é21é22í23ñ24ñ25’26ñ27ó28ú29’fghij";
?>
Thank you,
deDogs
Your charset in the HTML code may say ISO-8859-1 but are you sure you saved the data in ISO-8859-1? The data may be saved in UTF-8 or Windows CP-1252