Prevent Rails from encoding the ampersands in a URL when outputting JSON

余生长醉 提交于 2019-12-05 12:51:30

问题


I have the following code:

render json: { image: image }

Image has an attribute "url". Let's say it's:

https://blah.com/a?A=B&C=D

When rendering, this is what I get:

{"image":{"url":"https://blah.com/a?A=B\u0026C=D"}}

The ampersand is getting encoded as \u0026

Is there any way to avoid this encoding?


回答1:


Add to your application.rb file:

config.active_support.escape_html_entities_in_json = false



回答2:


your answer described there:

Converting URL to JSON version?

JSON encoding wrongly escaped (Rails 3, Ruby 1.9.2)

Why does is Rails 4 unicode escaping in to_json

Encoding JSON in href using encodeURI, Rails parsing (article)

and also this would help you:

Jbuilder



来源:https://stackoverflow.com/questions/27379432/prevent-rails-from-encoding-the-ampersands-in-a-url-when-outputting-json

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!