我的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;显示为"
。
因此,我有方法
decodeURIComponent
这是我尝试解码的字符串:
[{"description":"test1 ","filenameAndPath":"test1.pdf"},{"description":"test file","filenameAndPath":"test copy.pdf"},{"description":"","filenameAndPath":""},{"description":"","filenameAndPath":""}]
这段代码不断抛出错误Syntax error: Unexpected token &
我错过了一些明显的东西吗?
答案 0 :(得分:1)
这是 HTML 编码。
使用here中的这个技巧: -
var encoded = "[{"description":"test1 ","filenameAndPath":"test1.pdf"},{"description":"test file","filenameAndPath":"test copy.pdf"},{"description":"","filenameAndPath":""},{"description":"","filenameAndPath":""}]"
var elem = document.createElement('textarea');
elem.innerHTML = encoded;
var decoded = elem.value;
console.log(decoded);
有关HTML和网址编码的差异,请参阅here