Javascript:decodeURIComponent 拒绝工作

Javascript: decodeURIComponent refuses to work

本文关键字:工作 拒绝 decodeURIComponent Javascript      更新时间:2023-09-26

我得到了这个人。他将字符串解码为可读字符串。

decodeURIComponent("Invalid Ticker Name 'u0027t'u0027 at line: 3")
"Invalid Ticker Name 't' at line: 3"

现在看看这个。它告诉我们这个字符串(数组项)的来源实际上是一个字符串。

typeof decodeURIComponent(errorsArr[i])
"string"

但是当我尝试解码数组项时,它不起作用。

decodeURIComponent(errorsArr[i])
"Invalid Ticker Name 'u0027t'u0027 at line: 3"

怎么办?

我知道它是什么。 字符串中包含文字'u0027。 您可以使用以下命令在浏览器中验证此行为:

javascript:alert("'u0027");alert("''u0027");

当您输入它时:

Invalid Ticker Name 'u0027t'u0027 at line: 3

它替换了 unicode 字符。 为什么不能使用%27

问题已解决。

如代码中所写

// If the document was sent as 'application/javascript' or
// 'text/javascript', then the browser wraps the text in a <pre>
// tag and performs html encoding on the contents.  In this case,
// we need to pull the original text content from the text node's
// nodeValue property to retrieve the unmangled content.
// Note that IE6 only understands text/html

我已经开始将文件作为html/文本提供,这解决了问题。