我试图在服务器上保存一些数据,但发现一些编码问题。
这就是我发送我的对象到PHP(它工作正常,但我不认为contentType实际上是在做任何事情):
$.post(
'myfile.php',
{
contentType: "application/x-www-form-urlencoded;charset=utf-8",
data : myData
},
function (data, textStatus, jqXHR){
//some code here
}
);然后在PHP(myfile.php)中:
header('Content-Type: text/html; charset=utf-8');
$file = 'data/theData.txt'; //the file to edit
$current = file_get_contents($file); // Open the file to get existing content
$a2 = json_decode( $current, true );
$data = $_POST['data']; //the data from the webpage
$res = array_merge_recursive( $data, $a2 );
$resJson = json_encode( $res );
// Write the contents back to the file
file_put_contents($file, $resJson);
?>正如你所看到的,我正在获取文件的原始内容并解码json。然后我将结果与在json中重新编码之前从网页发送的数据进行合并,并将内容返回。
这一切都按预期工作。然而,在我的Jquery中的某一时刻,发送的数据包含了很多符号,如“/ö鳯ę”
当文件被保存时,每个'/'在它前面都有一个转义字符'\',同样的'é'例如是'\ u00e9'
我如何覆盖这个?我应该尝试在PHP中正确转换它,还是在JQuery中将它转换回正确的格式后,我有$ .get('data / theData.txt'?
任何关于这个问题的灯都非常感谢!请原谅可怜的变数名称。