RJ24 RJ24 - 6 months ago 29
Javascript Question

handling special characters in parameters with XMLHttpRequest

Trying to send password using

xmlHttpRequest
from frontend(javascript) with POST and other parameters with names like

"&password=" document.getElementById('password').value


I'm using HttpServletRequest.getparameter to get the parameter string of password like
string pswd = request.getparameter("password");


The code works fine with all passwords that have special characters except passwords like these qwe100%qwe, qwe198%qwe
When I pass those passwords, if I were to read username or other parameters sent along with password also results in exception[illegalArgumentException]
any help is appreciated..

Answer

You need to use encodeURIComponent on the JavaScript side before passing it to the back end.

That is:

var pass = encodeURIComponent(document.getElementById('password').value);

Note that on the Java side, you should then do a:

URLDecoder.decode(request.getParameter("password"));

EDIT

As @BalusC points out I'm wrong on the Java side - you have already done what is needed on the JavaScript side so you don't need the Java part.