6
votes

I've to read JSON file encoded with utf-8 charset
I use this syntax:

$http.get('resources/negozi.json',
    {header : {'Content-Type' : 'application/json; charset=UTF-8'}
}).success(function(data) {
... code here
});

But, the response header is:

Content-Type:text/plain; charset=ISO-8859-1

If I try to do this with jquery:

$.ajax({
type: "GET",
url: "resources/negozi.json",
contentType: "application/json; charset=utf-8",
dataType: "json",

The request header is correct. But, the response is the same.

Content-Type:text/plain; charset=ISO-8859-1

2
what's the server you're using? - nubbel
As @nubbel said, server response is the one that dictates what is returned - check your server-side code. - Qantas 94 Heavy
Server side code is me. I write an utf-8 encoded json by myself for testing. Unluckily I cannot change anything on server - Banasci
@Banasci If the server side code is yours why can't you just change it? - nubbel
If the server says charset=ISO-8859-1 and it is the truth - why would you care? These things are supposed work transparently, you shouldn't have to (and therefore can't easily) force the server to give you a certain charset. However, if the server says charset=ISO-8859-1 and it is not the truth, you need to fix the server, not the client. - Tomalak

2 Answers

6
votes

It looks like you might need to set an Accept header. The contentType header applies to the stuff you're sending;

Accept: application/json;charset=UTF-8
Accept-Charset: UTF-8

so;

$.ajax({
   type:"GET",
   url:"resources/negozi.json",
   headers: {
       "Accept": "application/json;charset=utf-8",
       "Accept-Charset":"charset=utf-8"
   },
   dataType:"json"
});      
0
votes

If you are using php as server-side programming language you can try to set the internal encoding to utf-8 with mb_internal_encoding("utf-8").