最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - angular.js $http.get how to force UTF-8 encoding - Stack Overflow

programmeradmin2浏览0评论

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

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

Share Improve this question edited Feb 16, 2015 at 10:31 Midhun KM 1,7171 gold badge21 silver badges31 bronze badges asked Oct 14, 2013 at 10:26 BanasciBanasci 811 gold badge1 silver badge6 bronze badges 6
  • what's the server you're using? – nubbel 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. – Qantas 94 Heavy 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 – Banasci Commented Oct 14, 2013 at 10:42
  • 1 @Banasci If the server side code is yours why can't you just change it? – nubbel 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. – Tomalak Commented Oct 14, 2013 at 11:59
 |  Show 1 more ment

2 Answers 2

Reset to default 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"
});      

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

发布评论

评论列表(0)

  1. 暂无评论