JavaScript解析包含德语字符的RSS

uajslkp6  于 5个月前  发布在  Java
关注(0)|答案(1)|浏览(47)

我正在尝试加载包含德语字符的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" />

js4nwp54

js4nwp541#

接收数据后,使用decodeURIComponent()将Unicode再次转换为字符文本。

xhrRequest.onreadystatechange = function() {
    if (xhrRequest.readyState == 4) {
        if (xhrRequest.status == 200) {
            var decodedResponse = decodeURIComponent(JSON.parse(xhrRequest.responseText));
    decodedResponce= decodedResponce.contents
            // Now decodedResponse contains the original characters
            console.log(decodedResponse);
        }
    }
}

字符串
如果您收到的响应不是JSON,

xhrRequest.onreadystatechange = function() {
    if (xhrRequest.readyState == 4) {
        if (xhrRequest.status == 200) {
            var decodedResponse = xhrRequest.responseText
            // Now decodedResponse contains the original characters
            console.log(decodedResponse);
        }
    }
}
``

相关问题