Pau*_*zel 16 javascript utf-8 character-encoding fetch-api
我正在尝试使用 Fetch 将一些数据带入屏幕,但是某些字符显示奇怪的 \xef\xbf\xbd 符号,我认为这与转换特殊字符有关。
\n\n在服务器端调试或在浏览器上调用 servlet 时,问题不会发生,因此我认为问题出在我的 JavaScript 上。请参阅下面的代码:
\n\nvar myHeaders = new Headers();\r\nmyHeaders.append(\'Content-Type\',\'text/plain; charset=UTF-8\');\r\n\r\nfetch(\'getrastreiojadlog?cod=10082551688295\', myHeaders)\r\n .then(function (response) {\r\n return response.text();\r\n })\r\n .then(function (resp) {\r\n console.log(resp);\r\n });Run Code Online (Sandbox Code Playgroud)\r\n我认为这可能是一些细节,但我还没有设法找出发生了什么。欢迎提供任何建议\n谢谢
\nTDT*_*TDT 37
响应的 text()函数始终将有效负载解码为 utf-8。
如果您想要其他字符集的文本,您可以使用TextDecoder将响应缓冲区(而不是文本)转换为具有所选字符集的解码文本。
使用你的例子它应该是:
var myHeaders = new Headers();
myHeaders.append('Content-Type','text/plain; charset=UTF-8');
fetch('getrastreiojadlog?cod=10082551688295', myHeaders)
.then(function (response) {
return response.arrayBuffer();
})
.then(function (buffer) {
const decoder = new TextDecoder('iso-8859-1');
const text = decoder.decode(buffer);
console.log(text);
});
Run Code Online (Sandbox Code Playgroud)
请注意,我正在使用iso-8859-1解码器。
学分:施耐德博客
| 归档时间: |
|
| 查看次数: |
27881 次 |
| 最近记录: |