Question

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 ?

Was it helpful?

Solution

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"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top