用PHP的json_encode来处理中文的时候, 中文都会被编码成Unicode
1 |
|
解决方式是加上JSON_UNESCAPED_UNICODE
1 |
|
在前面也要记得加上
mysqli_set_charset($conn,'utf8');
附前后台传递:
前台js:
$.ajax({
type:'post',
url:'http://localhost/lunbo.php',
contentType: 'application/json;charset=utf-8',
dataType:'text',
data:obj,
async:false,
complete:function()
{
alert("发送请求成功");
},
success:function(data)
{
alert(data);
$("#e").html(data);
},
error:function(XMLHttpRequest, textStatus, errorThrown) {
alert(XMLHttpRequest.status);
alert(XMLHttpRequest.readyState);
alert(textStatus);
},
})
后台php:
<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept');
header("Content-type: text/html; charset=utf-8");
date_default_timezone_set('prc');
$json=file_get_contents('php://input');
$json1=json_decode($json);
$jsonstr=json_encode($json1,JSON_UNESCAPED_UNICODE);
echo $jsonstr;
?>