XMLHttpRequest responseText 到 arrayBuffer,unicode U+FFFD

edw*_*ang 5 javascript unicode xmlhttprequest

呼叫的 responseText 充满了替换 unicode 字符 (U+FFFD) ?。我无法将 responseType 设置为 arraybuffer。 可以将 responseType 设置为“arraybuffer”的答案

有没有办法恢复已转换为字符串的原始二进制文件(原始数据)?

console.log(xhr.responseText);                // 'at?'
console.log(xhr.responseText.codePointAt(0)); // '97'
console.log(xhr.responseText.codePointAt(1)); // '116'
console.log(xhr.responseText.codePointAt(2)); // '65533'
Run Code Online (Sandbox Code Playgroud)

1) Javascript 将数据转换为 String 时数据会丢失吗?

或者

2)数据作为字符串是否安全,但我还没有找到将其解码为二进制/十六进制/十进制的正确方法?(在那种情况下,数据很好,但在打印时它只显示替换字符。)

小智 0

我真的很难处理同样的问题。我不知道正确的解决方案,但我确定的一件事是,当您拥有 \ufffd 字符时,您的数据就消失了,您无法发现它是什么,并且您绝对应该在此之前解决问题。...我还使用wireshark检查我实际接收到的数据,并且接收到的数据是正确的,所以问题可能发生在XMLHttpRequest对象周围的某个地方!