I am using a jQuery ajax command, which has the following data:

$.ajax({
type:"POST",
 ...
 data:"e=f_s&es="+JSON.stringify(email)+"&fr="+str
 ...
})

Where (email) can contain special character, for example it can be a string:

!#$%'&+-/=?^`*{|}~ch!#$%'/=?*^`{|}@mail.com

The reason why I allow such characters, is based on the following question.

The problem is, at some point on the server (Java EE application), it is messing up. The special characters are not showing the boundaries of different request parameters. For example it is considering :

'/

as a parameter.
I think I need to escape characters? (if yes how?)

What should I do to be able to send such a string from javascript to java ?

有帮助吗?

解决方案

Use encodeURIComponent:

encodeURIComponent("!#$%'&+-/=?^`*{|}~ch!#$%'/=?*^`{|}@mail.com")

returning:

"!%23%24%25'%26%2B-%2F%3D%3F%5E%60*%7B%7C%7D~ch!%23%24%25'%2F%3D%3F*%5E%60%7B%7C%7D%40mail.com"
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top