在输出JSON时,防止Rails对URL中的&符号进行编码

Dan*_*ola 19 encoding json ampersand ruby-on-rails-4

我有以下代码:

render json: { image: image }
Run Code Online (Sandbox Code Playgroud)

图像具有属性"url".让我们说它是:

https://blah.com/a?A=B&C=D
Run Code Online (Sandbox Code Playgroud)

渲染时,这就是我得到的:

{"image":{"url":"https://blah.com/a?A=B\u0026C=D"}}
Run Code Online (Sandbox Code Playgroud)

&符号被编码为\ u0026

有没有办法避免这种编码?

小智 35

添加到您的application.rb文件:

config.active_support.escape_html_entities_in_json = false
Run Code Online (Sandbox Code Playgroud)

  • 并且不要忘记在以下后重新启动服务器:-) ..nice answer! (3认同)
  • 如果您从 JSON https://brakemanscanner.org/docs/warning_types/cross_site_scripting_to_json/ 中的某个位置呈现数据,这将使您的应用程序面临 XSS 漏洞 (2认同)