[英]Angular 4 http.get receive “charset UTF-8” but change accented character to their ISO code counterpart
我第一次遇到这个问题,在网上找不到任何有关它的信息。
通过Angular Http API,我查询了IIS上托管的SQL Plus服务器,该服务器向我发送了带有以下标头的json: Content-Type:text/html; charset=utf-8
Content-Type:text/html; charset=utf-8
。 但是在我的应用程序中,接收到的带重音符的字符全部转换为它们的html代码副本:é=> é
,è=> è
等等。
这是我提出的示例请求:
let URI = '...';
return this._http.get(URI)
.map(
response => {
console.debug(response);
return data;
}
);
从Angular应用程序中登录时的结果摘录如下:
[
{
{
i: 0,
v: "97"
},
{
i: 1,
v: "éo"
},
{
i: 2,
v: "2;1"
},
{
i: 3,
v: "0"
}
}
]
但是,如果我直接使用Chrome,IE和Firefox查询服务器,方法是将完全相同的URL粘贴在导航栏中,则结果是正确的:
[
{
{
"i": 0,
"v": "97"
},
{
"i": 1,
"v": "é"
},
{
"i": 2,
"v": "2;1"
},
{
"i": 3,
"v": "0"
}
}
]
这两个请求具有完全相同的标头,因此我只能假定Angular的http API正在以某种方式更改响应,但我不知道如何更正此请求。 我尝试使用decoreURI和encodeURIComponent方法没有成功。
我希望有人已经遇到此问题并解决了吗?
编辑:我正在使用Angular 4.3.6和Angular CLI 1.2.1
我认为这个问题的答案是:
https://stackoverflow.com/a/43164489/4770754
是一个更好的方法:
发送请求时包括RequestOptionsArgs
。
在RequestOptionsArgs
指定字段responseType : ResponseContentType
。 ( ResponseContentType.ArrayBuffer
或ResponseContentType.Blob
)
使用TextDecoder
或类似方法解码结果。
参见文档:
https://angular.io/api/http/Http
https://angular.io/api/http/RequestOptions
https://angular.io/api/http/ResponseContentType
一旦以正确的编码获得了响应,您就可以按照以下方式将它们从ISO转换为UTF8:( https://stackoverflow.com/a/5396742/4770754 )
let utfString = decodeURIComponent(ajaxreturn.replace(/\\x/g,"%"));
但我怀疑到那时,您会很满意将它们原样显示在网站上。
在这种情况下,您可以将整个网站设置为您的编码:
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-2" />
:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.