Why is UTF-8 encoding lost after POST request?

时间:2016-10-19 13:40:41

标签: javascript php apache encoding 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 个答案:

没有答案
相关问题