dongyuan8312 2016-10-19 13:40
浏览 90

为什么POST请求后UTF-8编码丢失了?

I have a simple jquery ajax post cal to send some data to a server. The data contains characters in UTF-8.

Here is the code:

var data = {
    name: "Ländertrends"
}

var data_string = JSON.stringify(data);
console.log(data_string); //encoding still looks good here

$.ajax({
    type: "POST",
    url: "includes/savedashboard.php",
    data: { saveDashboard: data_string, dashboardname: name, conf: conf },
    contentType: "application/x-www-form-urlencoded; charset=utf-8",        

    success: function(result) {

        console.log(result);

    }
}); 

includes/savedashboard.php looks like this:

<?php
header('Content-Type: text/html; charset=UTF-8');
var_dump($_POST['saveDashboard']);
?>

On my development server (Ubuntu 14.04, Apache2, PHP 5.5.9), the output is fine and looks like you would expect:

string(24) "{"name":"Ländertrends"}"

However, on a second server (CentOS 6.8, Cpanel, Apache2, PHP 5.4.45), the encoding is messed up:

string(25) "{"name":"L���ndertrends"}"

I can see the content type of the request comes back as UTF-8.

If I use mb_detect_encoding() it says the string is in EUC-JP.

My guess is that some system setting is making the difference, but which one?

I have already set the content type, also added AddDefaultCharset UTF-8 to a .htaccess file, and added charset="utf-8" to my script tags, but it doesn't seem to make a difference.

Can anyone point me in the right direction?

By the way, I can work around the problem by converting the string back to UTF-8, but I would like to know why this is happening on one server and not the other.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 Jenkins自动化部署—悬赏100元
    • ¥15 关于#python#的问题:求帮写python代码
    • ¥20 MATLAB画图图形出现上下震荡的线条
    • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
    • ¥15 perl MISA分析p3_in脚本出错
    • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
    • ¥15 ubuntu虚拟机打包apk错误
    • ¥199 rust编程架构设计的方案 有偿
    • ¥15 回答4f系统的像差计算
    • ¥15 java如何提取出pdf里的文字?