views:

442

answers:

2

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?

+6  A: 

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.

Sean Kinsey
thanks, but for some reason the "URI.escape(foo, Regexp.new([^#{URI::PATTERN::UNRESERVED}]"))" isn't working for me, it gives an error and the page doesn't load when it calls it. i have require 'uri' at the top of the class. thanks
Mo
I only tried it in a .rb file, worked like a charm there..
Sean Kinsey
just got it working, me being the idiot that i am was having the Regexp.new("[^#{URI::PATTERN::UNRESERVED}]") in the unescape as well... that what happens when u have been looking at code for 10 hrs straight. thanks very much @sean it all works properly now
Mo
HI again, after some selenium testing i noticed that for some reason the URI.unescape mixes up between the "£" and the "?" , in my test i if i encodeURIComponent("£"); in java script and then URI.unescape("%C2%A3") which is the value we get when we encode the "£" sign, i get a return of the "?" sign, any work around? thanks
Mo
+2  A: 

In URL encoding, both + and %20 represent a space, so even though one may encode the space into a + and the other into a %20, they should both happily decode either back into a space.

Javascript has a couple of different functions for encoding things for transmission; the best one to use is encodeURIComponent, which encodes each component of a URI. Like so:

var name = /* ...get the name from somewhere, perhaps a form field... */;
var queryString = "?name=" + encodeURIComponent(name);

This is better than the previous encodeURI function (and several people mistakenly think that escape does this, but it doesn't; it does something else entirely which is of fairly limited use).

T.J. Crowder
(Aside: `+` only represents a space in `application/x-www-form-urlencoded` content, such as a query parameter name or value: `?a+b=c+d`. In the rest of the URL it represents a plus sign. To avoid this confusion it's a good idea to always encode spaces to `%20` and plusses to `%2B`, which is what `encodeURIComponent` does. `escape` is indeed wrong for additional reasons, but we seem to be unable to kill the damned thing!)
bobince
@bobince: Thanks, good clarification.
T.J. Crowder
I would personally like to track down the person that gave us '+' for encoding and say 'shame on you' to them. ugh.
Tom Andersen