2

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 ?

Community
  • 1
  • 1
ccot
  • 1,875
  • 3
  • 36
  • 54
  • I've had a similar problem, see my answer here: http://stackoverflow.com/a/34345900/1864614 – razvang Dec 18 '15 at 11:55
  • also in the future, java and javascript are very different; this question has nothing to do with java. just something to keep in mind; glad you got an answer – AwesomeDude3247 Apr 19 '21 at 19:33

1 Answers1

3

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"
Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674