使用AJAX和jQuery时,Firefox在编码方面遇到一些大问题。
我用$.ajax()传递了一个字符串,在php代码中,我使用的函数是:
header("Content-Type: text/html; charset=ISO-8859-1",true);
jQuery:
$.ajax({
type: 'GET',
url: 'Filme_comparador_horarios.php',
data: 'cartaz='+$filme_compara,
success: function(retorno)
{
$('#cartaz_comp').append(retorno);
}
PHP:
if(isset($_GET["cartaz"]))
{
$cartaz = $_GET["cartaz"];
echo"
<div class='cartaz_comp_img'><img class='cartaz_comp_imagem' src='horarios/$cartaz/filme.jpg' width='140px' height='210px'/>
<div class='nome_comp'>$cartaz</div>
</div>
";
}
我已经尝试使用echo utf8_decode($ cartaz);使其在Firefox中运行正常,但在IE和Chrome中仍无法使用。
Try this :
$.ajax({
type: 'GET',
url: 'Filme_comparador_horarios.php',
data: 'cartaz='+$filme_compara,
contentType: 'text/html;charset=ISO-8859-1',
success: function(retorno)
{
$('#cartaz_comp').append(retorno);
}
You can try with htmlspecialchars in PHP, you can use the code below,
echo htmlspecialchars("
<div class='cartaz_comp_img'><img class='cartaz_comp_imagem' src='horarios/$cartaz/filme.jpg' width='140px' height='210px'/>
<div class='nome_comp'>$cartaz</div>
</div>
");
and in your Ajax page create this javascript function
function htmlspecialcharsDecode(specialChars) {
specialChars = specialChars.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
return specialChars;
}
in your ajax code
success: function(retorno)
{
$('#cartaz_comp').append(htmlspecialcharsDecode(retorno));
}