【发布时间】:2018-05-12 07:55:47
【问题描述】:
我的网站使用 https,当我尝试使用 ajax 获取数据时,给了我错误的字符。我的字符集是:meta content="text/html; charset=utf-8"
$.ajax({
type:'get',
url:'{!!URL::to('findCiudad')!!}',
data:{'departamento':departamento},
beforeSend : function(xhr) {
xhr.setRequestHeader('Accept', "text/html; charset=utf-8");
},
success:function(data){
op1+='<option value="0" selected disabled>Select City</option>';
for(var i=0;i<data.length;i++){
op1+='<option value="'+data[i].local+'">'+data[i].desc+'</option>';
}
$("#ciudad_entrega").html(" ");
$("#ciudad_entrega").append(op1);
},
error:function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
}
});
这是我的控制器,用 utf8_encode 转换结果
$result = array();
foreach ($datos as $key => $dato) {
//return $key;
array_push($result,
array(
'localidad' => utf8_encode($dato->localidad),
'descripcion' => utf8_encode($dato->descripcion),
'oficina' => utf8_encode($dato->oficina),
'cod_departamento' => utf8_encode($dato->cod_departamento)
)
);
}
return response()->json($result, 200, ['Content-type'=> 'application/json; charset=utf-8']);
【问题讨论】:
-
utf8_encode()/utf8_decode()实际上只有一种用途,那就是在 ISO8859-1 和 UTF8 之间进行转换。 不要将这些函数用于其他任何事情,或者如果您不确定输入编码是 8859-1,否则您将损坏您的数据。 stackoverflow.com/questions/279170/utf-8-all-the-way-through
标签: php jquery json utf-8 https