[英]Javascript string encoding Windows-1250 to UTF8
I've an angularjs app that receive data from an external webservice. 我有一个angularjs应用,可从外部Web服务接收数据。
I think I'm receiving UTF-8 string but encoded in ANSI. 我想我正在接收UTF-8字符串,但使用ANSI编码。
For example I get 例如我得到
KLMÄšLENÃ
When I want to display 当我想显示
KLMĚLENÍ
I've tried to use decodeURIComponent to convert it but that doesn't work. 我试图使用decodeURIComponent对其进行转换,但这不起作用。
var myString = "KLMÄšLENÃ"
console.log(decodeURIComponent(myString))
I'm probably missing something but I can't find what. 我可能缺少了一些东西,但找不到。
Thanks and regards, Eric 谢谢和问候,埃里克
You can use TextDecoder . 您可以使用TextDecoder 。 (Be beware! some browser does not support it.)
(请注意!某些浏览器不支持它。)
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 code for simulating server side output: 用于模拟服务器端输出的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.