decodeURIComponent上的意外令牌

时间:2016-11-15 20:32:07

标签: javascript html grails

我的grails视图中有一个参数,我将其传递给javascript代码

<g:if test="${replacedString}">
    ${replacedString}
    <g:set var="replacedString" value="${replacedString}"/>
</g:if>
<script>
   var mydata = decodeURIComponent(${replacedString});
   console.log(mydata);
</script>

replacementString值作为编码字符串传递,即:&#34;显示为&quot;。 因此,我有方法

decodeURIComponent

这是我尝试解码的字符串:

[{&quot;description&quot;:&quot;test1 &quot;,&quot;filenameAndPath&quot;:&quot;test1.pdf&quot;},{&quot;description&quot;:&quot;test file&quot;,&quot;filenameAndPath&quot;:&quot;test copy.pdf&quot;},{&quot;description&quot;:&quot;&quot;,&quot;filenameAndPath&quot;:&quot;&quot;},{&quot;description&quot;:&quot;&quot;,&quot;filenameAndPath&quot;:&quot;&quot;}]

这段代码不断抛出错误Syntax error: Unexpected token & 我错过了一些明显的东西吗?

1 个答案:

答案 0 :(得分:1)

这是 HTML 编码。

使用here中的这个技巧: -

var encoded = "[{&quot;description&quot;:&quot;test1 &quot;,&quot;filenameAndPath&quot;:&quot;test1.pdf&quot;},{&quot;description&quot;:&quot;test file&quot;,&quot;filenameAndPath&quot;:&quot;test copy.pdf&quot;},{&quot;description&quot;:&quot;&quot;,&quot;filenameAndPath&quot;:&quot;&quot;},{&quot;description&quot;:&quot;&quot;,&quot;filenameAndPath&quot;:&quot;&quot;}]"

var elem = document.createElement('textarea');
elem.innerHTML = encoded;
var decoded = elem.value;

console.log(decoded);

有关HTML和网址编码的差异,请参阅here