How do I raw URL encode/decode in JavaScript and Ruby to get the same values in both?

后端 未结 3 1586
旧巷少年郎
旧巷少年郎 2020-12-13 11:09

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

相关标签:
3条回答
  • 2020-12-13 11:30
    require "uri";
    puts URI.escape "1 2;", Regexp.new("[^0-9a-z\\-_.!~*'()]", "i");
    
    0 讨论(0)
  • 2020-12-13 11:39

    URI.escape was deprecated, an alternative is ERB::Util.url_encode.

    ERB::Util.url_encode(foo)
    

    If you are using it inside an .erb file you can do:

    u(foo)
    
    0 讨论(0)
  • 2020-12-13 11:46

    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.

    0 讨论(0)
提交回复
热议问题