Javascript 解析包含德文字符的 RSS

问题描述 投票:0回答:1

我正在尝试加载包含德语字符的 rss (ö, ä, ü...)

xhrRequest.responseText 已包含转换后的字符 (\u00f ...)

无需手动替换,是否可以强制正确编码以使原始字符保持可见?

var url = 'https://apolut.net/feed/podcast/'

    xhrRequest = new XMLHttpRequest();
    xhrRequest.onreadystatechange = function() {
        if (xhrRequest.readyState == 4) {

            
        }
    }
    xhrRequest.onerror = function(e) { 
       
    };
    xhrRequest.open('GET', url);
    xhrRequest.setRequestHeader("Content-Type", "text/xml");
    xhrRequest.send();

我的页面顶部确实有 utf:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
javascript character-encoding xmlhttprequest
1个回答
0
投票

收到数据后,使用

decodeURIComponent()
再次将Unicode转为带有该字符的文本。

xhrRequest.onreadystatechange = function() {
    if (xhrRequest.readyState == 4) {
        if (xhrRequest.status == 200) {
            var decodedResponse = decodeURIComponent(JSON.parse('"' + xhrRequest.responseText + '"'));
            // Now decodedResponse contains the original characters
            console.log(decodedResponse);
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.