Json Special Characters Error

Asked

Viewed 1,611 times

0

I have a script where, after selecting the first select it returns me in the second select the currency referring to the country, but these countries and coins when you have special requirements are bringing me "B Snia and Herzegovina"

script js.

$(document).ready(function () {
var paises = null;
var moedas = null;
$.ajax({
    type: "GET", 
    url: "../paises.json",
    contentType: "application/json ; charset=UTF-8",
    cache: false,
    success: function(retorno) {
            paises = retorno;
            $.each(paises,function(i, pais){
                $('#pais').append($('<option>', {
                                    value: paises[i].sigla_moeda,
                                    text: paises[i].pais
                                }));
            });
    } 
});
$('#pais').change(function(){
    $("#moeda").empty();
    $.ajax({
    type: "GET", 
    url: "../moedas.json",
    contentType: "application/json ; charset=UTF-8",
    cache: false,
    success: function(retorno) {
            var moedas = retorno;
            $.each(moedas,function(i, moeda){
                if($('#pais').val() === moedas[i].sigla){
                    $('#moeda').append($('<option>', {
                                value: moedas[i].sigla_moeda,
                                text: moedas[i].moeda
                            }));
                }
            });
    } 
});
})  

thanks in advance!

1 answer

0


Your page must be using UTF-8 and your JSON must have been saved as iso-8859-1/ANSI, do the following, open both json paises.json in sublimetext or Notepad++ and save them as UTF-8:

  • To save using Sublimetext:

    salvando documento em ANSI com notepad++

  • To save using Notepad++:

    salvando documento em iso-8859-1 ou windows 1252

Still note that perhaps it would be better if the accents were escaped with \u, that is, who generated the files should have done this to avoid losses in the accents, as for example:

{ "cidade": "S\u00e3o Paulo" }

Then Javascript itself would interpret this and you wouldn’t even need to escape anything.

Just for the record, content-type in requisitions HTTP does not change the encoding of HTTP response:

contentType: application/json; charset=UTF-8

That is, it only affects if you send JSON via payload with POST, it will not affect at all the response received after sending.

  • Dear you saved me, thank you very much. I followed your clarification and removed the contenttype. Thanks!

  • @Ericoliveira, would you please mark the answer as correct? Stay on the left side of the answer if you don’t know how to do it read the instructions at: https://pt.meta.stackoverflow.com/q/1078/3635

Browser other questions tagged

You are not signed in. Login or sign up in order to post.