[英]Javascript string encoding Windows-1250 to UTF8
我有一个angularjs应用,可从外部Web服务接收数据。
我想我正在接收UTF-8字符串,但使用ANSI编码。
例如我得到
KLMÄšLENÃ
当我想显示
KLMĚLENÍ
我试图使用decodeURIComponent对其进行转换,但这不起作用。
var myString = "KLMÄšLENÃ"
console.log(decodeURIComponent(myString))
我可能缺少了一些东西,但找不到。
谢谢和问候,埃里克
您可以使用TextDecoder 。 (请注意!某些浏览器不支持它。)
var xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.responseType = 'arraybuffer';
xhr.onload = function() {
if (this.status == 200) {
var dataView = new DataView(this.response);
var decoder = new TextDecoder("utf-8");
var decodedString = decoder.decode(dataView);
console.log(decodedString);
} else {
console.error('Error while requesting', url, this);
}
};
xhr.send();
用于模拟服务器端输出的Java Servlet代码:
resp.setContentType("text/plain; charset=ISO-8859-1");
OutputStream os = resp.getOutputStream();
os.write("KLMĚLENÍ".getBytes("UTF-8"));
os.close();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.