0

I have a URL like https://<domainname>/popup.jsp?sitekey=7-8&type=popup of this the request parameter string (sitekey=7-8&type=popup) is formed dynamically and then appended to the request URL and then a AJAX request is submitted. Before attaching the request parameter string to the URL we use escape() function of JS. so its like :

var URL=/popup.jsp?;
var reqVal="sitekey=7-8";
reqVal=reqVal+"&type=popup";
URL=URL+escape(reqVal);

So the URL actually is submitted as

/popup.jsp?sitekey%3D7-8;amp&type%3Dpopup

In servlet when loop through the request parameter names I get parameter name as sitekey%3D7-8 instead of only sitekey and hence my code breaks when I try to get value of sitekey by using request.getParameter("sitekey")

Please help as to how I decode the URL so that request parameter name I get is proper.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Mukesh
  • 182
  • 1
  • 2
  • 10

1 Answers1

1

Instead of escape use encodeURI.

The escape() function was deprecated in JavaScript version 1.5.

What is the difference?

escape — broken, deprecated, do not use, encodes all the characters.

encodeURI — encodes characters that are not allowed (raw) in URLs (use it to fix up broken URIs if you can't fix them beforehand)

void
  • 36,090
  • 8
  • 62
  • 107
  • Thanks for the reply.I will use encodeURI but my question is when I encode the entire URI whose result is /popup.jsp?sitekey%3D7-8;amp&type%3Dpopup how am I going to read the request parameters because when i read the request parameter names in servlet I get parameter name as sitekey%3D7-8;amp&type%3D instead of getting sitekey and type as two different parameter names or do you mean to say that encodeURI will encode only the param values and not the param names – Mukesh Dec 02 '15 at 07:56
  • @Mukesh, when you will use `encodeURI` the `?` and `=` won't encode, as they are URL safe chars. So the key will be `sitekey` only. – void Dec 02 '15 at 08:21
  • I got it.Thanks for the your help. – Mukesh Dec 02 '15 at 10:57