jQuery angular.js $http.get 如何强制使用 UTF-8 编码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19358193/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
angular.js $http.get how to force UTF-8 encoding
提问by Banasci
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
内容类型:文本/普通;字符集=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
内容类型:文本/普通;字符集=ISO-8859-1
回答by cirrus
It looks like you might need to set an Accept
header. The contentType
header applies to the stuff you're sending;
看起来您可能需要设置Accept
标题。该contentType
头适用于你发送的东西;
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"
});
回答by Patrick Vogt
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 作为服务器端编程语言,您可以尝试将内部编码设置为 utf-8 mb_internal_encoding("utf-8")
。