php - 通过 ajax 提交时,德语特殊字符卡住

标签 php jquery ajax utf-8

我正在通过 jquery.ajax 向结果 div 提交参数(包括特殊字符,如 ä、ö、ü)。在那个 div 中,我需要用 php 处理它。

例如:

$( document ).ready(function() {
    $('#dropdown').change(function() {
        $.ajax({
            url: "inc/ajax.results.php",
            type: "GET",
            data: 'type='+$('#type').val()
        }).done(function(data){
            $("#results").html(data);
        });
    });
});

在此示例中,“type”的值为“Müller”。在我的“ajax.results.php”中,我这样做:

<?= $_GET['type'] ?>

// Output is 'Müller' in Firefox and Chrome

// BUT in internet explorer the output is 'M'

因此,对于 Firefox 和 Chrome 来说没问题,但在 Internet Explorer 中,结果是“M”(M 后跟一个方 block )...

我试过像这样改变输出:

<?= utf8_encode($_GET['type'] ?>

// Output in internet Explorer now is fine (Müller)

// BUT in Firefox and Chrome it is 'Müller'

由于输出必须通过 PHP(因为我将用它做进一步的操作),我找不到解决方案...

谁能帮忙解决这个问题? 非常感谢

最佳答案

在有下拉菜单的 HTML 页面上,插入

<meta charset="utf-8"> 

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

在标签内。

并确保将所有文件保存为 UTF-8(或更好:不带 BOM 的 UTF-8)

Apache 服务器默认配置为提供 ISO-8859-1 中的文件,因此您需要将以下行添加到您的 .htaccess 文件中:

AddDefaultCharset UTF-8

关于php - 通过 ajax 提交时,德语特殊字符卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28041029/

相关文章:

php - 存储过程中的 OUT 参数声明

php - 在 PHP 中使用 UTF-16BE 编码保存 CSV

php - 返回 PDO 数据

jquery - 如何在单击 HTML 按钮时呈现部分页面?

javascript - 如果列只有一项,如何删除标签

javascript Internet Explorer 不会检测 window.close

php - 按价格顺序对数据库进行排序的单选按钮突然停止工作

javascript - 为什么从 focus() 改为触发 ('focus' )?

javascript - axios假404错误

php - ajax 请求的数据未存储到 $_POST 数组中