Words with an accent do not work

1

I have Ajax which loads data from the database and in type json:

$.ajax({
    type:'post',
    dataType: 'json',
    url: '../model/dao/CursoDao.php?acao=1',
    success: function(dados){

        for(var i=0; dados.length > i; i++){

            $('#listaCursos').append('<a href="sala_view.php?idcurso='+ dados[i].id + '" class="btn btn-primary texto-grande botao-nivel sombra">' + dados[i].titulo +'</a>');

        }
    }
});

The PHP function:

  public function listaCursoAjax() {

$sqlCurso = $this->conexao->prepare('SELECT * FROM curso');
$sqlCurso->execute();

while($linha = $sqlCurso->fetch(PDO::FETCH_ASSOC)) {
  $vetor[] = array_map('utf8_encode',$linha);
}

echo json_encode($vetor);

}

The problem is that phrases with an accent are being encoded:

    
asked by anonymous 24.08.2017 / 23:33

1 answer

2

Try using the following code:

    public function listaCursoAjax() {

    $sqlCurso = $this->conexao->prepare('SELECT * FROM curso');
    $sqlCurso->execute();

    while($linha = $sqlCurso->fetch(PDO::FETCH_ASSOC)) {
        foreach($linha as &$result){
            $result = utf8_encode($result);
        }
        $vetor[] = $linha;
    }


echo json_encode($vetor);
    
25.08.2017 / 00:17