Encodeuricomponent decoding it in rails

2019-06-24 02:01发布

Normally rails magically decodes all params. Now I got a javascript which does params="value="+encodeURIComponent('ab#cd'); and then calls http://server/controller?value=ab%23cd. If I access params[:value] in my controller, it contains ab%23cd and not ab#cd as I would expect.

How to solve this? Why does rails no auto decoding of this param?

1条回答
霸刀☆藐视天下
2楼-- · 2019-06-24 02:32

Rails "automagically" handles parameters with the following logic.

If the request is GET it will decode anything in the query string:

GET http://server/controller?value=ab%23cd
  On the server this will generate params['value'] as ab#cd

If the request is a POST with a query string it will not decode it:

POST http://server/controller?value=ab%23cd
  On the server this will generate params['value'] as ab%23cd

If the request is a POST with data parameters, it will decode it:

POST http://server/controller
  data: value=ab%23cd
  On the server this will generate params['value'] as ab#cd

I suspect that you're seeing this issue because you're including a query string with a POST request instead of a GET request and so Rails isn't decoding the query string.

查看更多
登录 后发表回答