【问题标题】:printing text with charset utf-8 over https doesnt print characters correctly通过 https 使用 charset utf-8 打印文本无法正确打印字符
【发布时间】: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


【解决方案1】:

我已经解决了,你必须转换:mb_convert_encoding()

mb_convert_encoding($dato->desc, 'UTF-8', 'UTF-8');

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-20
    • 2020-02-03
    • 2016-10-10
    • 1970-01-01
    • 2013-04-30
    • 1970-01-01
    • 2011-01-08
    相关资源
    最近更新 更多