开发者

escaping a value in javascript when submitting form onclick

开发者 https://www.devze.com 2023-03-09 18:01 出处:网络
I am sending sensitive data encrypted when the user clicks the onclick event.This encrypted data at times contains a plus sign (+) When I retrieve this request variable on the server, the + is getting

I am sending sensitive data encrypted when the user clicks the onclick event. This encrypted data at times contains a plus sign (+) When I retrieve this request variable on the server, the + is getting converted to a whitespace. This causes the decr开发者_如何学运维yption to fail.

Example:

xrUxHtYpO2Yu3Z31ve+KNA==

gets converted to:

xrUxHtYpO2Yu3Z31ve KNA==

Is there a way escape the string so it is sent as is?


The function you're looking for is "encodeURIComponent()":

var encoded = encodeURIComponent("nasty string");

You shouldn't need any code at all on the server side; URL encoding will almost certainly be implicitly un-done by your web framework. (Edit - ah, if you're using some Java/JSP web framework, then you definitely don't have to do anything fancy on the server side.)


Try replacing the + with %2B. That came from HTML URL Encoding Reference at W3Schools. Hope this helps!

0

精彩评论

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