我的网站有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']);
答案 0 :(得分:-1)
我已经解决了,你必须转换为:mb_convert_encoding()
mb_convert_encoding($ dato-&gt; desc,'UTF-8','UTF-8');