Json Special Characters Error

0

I have a script where, after selecting the first select it returns me in the second select the country currency, but those countries and currencies when you have special characters are bringing me "Bosnia 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
                            }));
                }
            });
    } 
});
})  

Thank you!

    
asked by anonymous 03.07.2018 / 06:01

1 answer

0

Your page should be using UTF-8 and your JSON should 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:

  • Tosaveusingnotepad++:

Notethatitmightbebetteriftheaccentswereescapedwith\u,thatis,whogeneratedthefilesshouldhavedonesotoavoidlosingaccents,suchas:

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

So JavaScript would interpret this and you would not need to escape anything.

Just to note, content-type HTTP requests do not change the HTTP response encoding:

contentType: application/json; charset=UTF-8

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

    
03.07.2018 / 06:16