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

时间:2022-05-26 21:43:17

I have the following code:

我有以下代码:

render json: { image: image }

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

图像具有属性“url”。让我们说它是:

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

&符号被编码为\ u0026

Is there any way to avoid this encoding?

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

2 个解决方案

#1


26  

Add to your application.rb file:

添加到您的application.rb文件:

config.active_support.escape_html_entities_in_json = false

#2


0  

your answer described there:

你在那里描述的答案:

Converting URL to JSON version?

将URL转换为JSON版本?

JSON encoding wrongly escaped (Rails 3, Ruby 1.9.2)

错误地转义了JSON编码(Rails 3,Ruby 1.9.2)

Why does is Rails 4 unicode escaping in to_json

为什么Rails 4 unicode在to_json中转义

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

使用encodeURI在Href中编码JSON,Rails解析(文章)

and also this would help you:

这也会对你有所帮助:

Jbuilder

在Jbuilder

#1


26  

Add to your application.rb file:

添加到您的application.rb文件:

config.active_support.escape_html_entities_in_json = false

#2


0  

your answer described there:

你在那里描述的答案:

Converting URL to JSON version?

将URL转换为JSON版本?

JSON encoding wrongly escaped (Rails 3, Ruby 1.9.2)

错误地转义了JSON编码(Rails 3,Ruby 1.9.2)

Why does is Rails 4 unicode escaping in to_json

为什么Rails 4 unicode在to_json中转义

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

使用encodeURI在Href中编码JSON,Rails解析(文章)

and also this would help you:

这也会对你有所帮助:

Jbuilder

在Jbuilder