[英]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.