angular.js $http.了解如何强制使用 UTF-8 编码

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

本文关键字:UTF-8 编码 何强制 js http 了解 angular      更新时间:2023-09-26

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

$http.get('resources/negozi.json',
    {header : {'Content-Type' : 'application/json; charset=UTF-8'}
}).success(function(data) {
... code here
});

但是,响应标头为:

内容类型:文本/纯文本;字符集=ISO-8859-1

如果我尝试使用 jquery 执行此操作:

$.ajax({
type: "GET",
url: "resources/negozi.json",
contentType: "application/json; charset=utf-8",
dataType: "json",

请求标头正确。但是,反应是相同的。

内容类型:文本/纯文本;字符集=ISO-8859-1

看起来您可能需要设置Accept标头。contentType标题适用于您要发送的内容;

Accept: application/json;charset=UTF-8
Accept-Charset: UTF-8

所以;

$.ajax({
   type:"GET",
   url:"resources/negozi.json",
   headers: {
       "Accept": "application/json;charset=utf-8",
       "Accept-Charset":"charset=utf-8"
   },
   dataType:"json"
});      
如果您

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