6

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

6
  • what's the server you're using? Commented Oct 14, 2013 at 10:32
  • 1
    As @nubbel said, server response is the one that dictates what is returned - check your server-side code. Commented Oct 14, 2013 at 10:37
  • Server side code is me. I write an utf-8 encoded json by myself for testing. Unluckily I cannot change anything on server Commented Oct 14, 2013 at 10:42
  • 1
    @Banasci If the server side code is yours why can't you just change it? Commented Oct 14, 2013 at 11:22
  • 1
    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. Commented Oct 14, 2013 at 11:59

2 Answers 2

6

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"
});      
Sign up to request clarification or add additional context in comments.

Comments

0

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").

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.