2016-11-15 81 views
1

意外的標記我有一個參數在我的Grails查看哪些我傳遞給JavaScript代碼上decodeURIComponent

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

的replacedString值被作爲編碼字符串,即通過:「顯示爲&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

這是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);

here是HTML格式的差異和URL編碼

+0

謝謝!不知道HTML和URL編碼的區別。學到了新的東西:) – shanwar