角。js美元http。获取如何强制UTF-8编码

时间:2023-01-06 08:13:48

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

我必须阅读用utf-8字符集编码的JSON文件,我使用以下语法:

$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

内容类型:文本/平原;charset = iso - 8859 - 1

If I try to do this with jquery:

如果我尝试用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

内容类型:文本/平原;charset = iso - 8859 - 1

2 个解决方案

#1


6  

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

看起来您可能需要设置一个Accept标头。contentType header应用于发送的内容;

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

#2


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

如果使用php作为服务器端编程语言,可以尝试使用mb_internal_encoding(“utf-8”)将内部编码设置为utf-8。

#1


6  

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

看起来您可能需要设置一个Accept标头。contentType header应用于发送的内容;

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

#2


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

如果使用php作为服务器端编程语言,可以尝试使用mb_internal_encoding(“utf-8”)将内部编码设置为utf-8。