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

German special characters get stuck when submitting via ajax

我正在通过 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 后跟一个正方形)...

我试过像这样更改输出:

<?= 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

感谢大家的帮助。

我自己找到了解决方案:我已将 'encodeURIComponent()' 添加到我的 ajax 请求中并且有效:-)

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