我收到一个字符串作为对get请求的响应
{"revision"=>"r2407", "full_version"=>"2.5 [r2407]", "full_name"=>" [r2407]", "version"=>"2.5"}
在js文件中:
$('.output').text('<%=CGI.unescape(@response.to_s)%>')
我仍然使用"
和东西获得相同的字符串。我也尝试过JS unescape(),它也没有做任何事情。怎么了?
答案 0 :(得分:0)
CGI.escape
和CGI.unescape
用于处理网址编码的字符串。
CGI.escape('&')
# => "%26"
CGI.unescape('%26')
# => "&"
改为使用CGI::unescape_html
:
CGI.unescape_html('>')
# => ">"
body = '{"revision"=>"r2407", "full_version"=>"2.5 [r2407]", "full_name"=>" [r2407]", "version"=>"2.5"}'
CGI.unescape_html(body)
# => {"revision"=>"r2407", "full_version"=>"2.5 [r2407]", "full_name"=>" [r2407]", "version"=>"2.5"}
答案 1 :(得分:0)
我认为您首先需要使用CGI.unescape_html
从响应中取消HTML,然后使用j
将其转义为JavaScript:
$('.output').text('<%=j CGI.unescape_html(@response.to_s)%>')