How do I raw URL encode/decode in JavaScript and R

2019-01-21 20:56发布

I am working on a web application where I have to encode and decode a string at the JavaScript side and Ruby backend of the code. the only problem is that the escape methods for JavaScript and Ruby have a small difference. in JavaScript the " " is treated as "%20" but in ruby the " " is encoded to "+".

Any way to solve this? Another Ruby method to encode a string in raw URL encode?

After some Selenium testing I noticed that for some reason the URI.unescape mixes up between the "£" and the "?". If I use encodeURIComponent("£"); in JavaScript and then URI.unescape("%C2%A3") in Ruby which is the value we get when we encode the "£" sign, I get the "?" sign returned. Any solution?

2条回答
Emotional °昔
2楼-- · 2019-01-21 21:28
require "uri";
puts URI.escape "1 2;", Regexp.new("[^0-9a-z\\-_.!~*'()]", "i");
查看更多
别忘想泡老子
3楼-- · 2019-01-21 21:38

Use

URI.escape(foo, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))

in ruby, and

encodeURIComponent(foo); 

in javascript

Both these will behave equally and encode space as %20.

查看更多
登录 后发表回答