Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 313
Banasci Avatar asked Oct 14 '13 10:10

Banasci


1 Answers

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"
});      
like image 198
cirrus Avatar answered Oct 13 '22 00:10

cirrus