Firefox中的编码问题

时间:2011-12-21 02:01:00

标签: php jquery ajax utf-8 iso-8859-1

使用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中打破。

2 个答案:

答案 0 :(得分:0)

试试这个:

$.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);
    }

答案 1 :(得分:0)

你可以在PHP中尝试使用htmlspecialchars,你可以使用下面的代码,

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>
    ");

并在您的Ajax页面中创建此javascript函数

function htmlspecialcharsDecode(specialChars) {
    specialChars = specialChars.replace(/&/g, "&amp;")
      .replace(/</g, "&lt;")
   .replace(/>/g, "&gt;")
  .replace(/"/g, "&quot;")
  .replace(/'/g, "&#039;");

   return specialChars;
}
你的ajax代码中的

    success: function(retorno)
{
    $('#cartaz_comp').append(htmlspecialcharsDecode(retorno));
}
相关问题