1

I have my website with https and when i try to get data with ajax, gave me wrong characters. my charset is: 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);
        }
    });

this is my controller, convert the result with 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']);
Leoh
  • 642
  • 2
  • 17
  • 41
  • There's is literally only one use for `utf8_encode()`/`utf8_decode()` and that's converting between ISO8859-1 and UTF8. *Do not use these functions for anything else*, or if you're not certain that the input encoding is 8859-1, otherwise you're going to corrupt your data. https://stackoverflow.com/questions/279170/utf-8-all-the-way-through – Sammitch Nov 28 '17 at 21:27

1 Answers1

-1

i already solved, you have to convert with: mb_convert_encoding()

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

Leoh
  • 642
  • 2
  • 17
  • 41