开发者

Encoding problems when passing parameters in url

I'm writing a piece of Javascript that takes in the title of the current page, and sends it to a form in a Rails 3 app. If the title is something like

Review - “Episode开发者_JAVA百科 20”

then when I pass that title to my form:

http://localhost:3000/notes/myform?title=Review - “Episode 20”

the title appears in my form as

Review - �Episode 20�

How do I fix this?


You can use encodeURI

console.log(encodeURI('Review - “Episode 20”')); 
// "Review%20-%20%E2%80%9CEpisode%2020%E2%80%9D"

I'm no Ruby pro, but I believe the Ruby way to decode this is CGI::unescape()


Or use URI_escape:

enc_uri = URI.escape("http://example.com/?a=\11\15")
# => "http://example.com/?a=%09%0D"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜