How to Encode String for Transmission to Server in JavaScript

Encode Text

Sometimes, you will see unexpected results in server when parsing POST or GET strings from JavaScript like the following snippet, it could be sourced from URL encoding problem.

...
params = "hashed_no=" + no + "&name=" + name;
...
request.send(params);
...

You can resolve the problem by adding an escape function to encode the data string:

...
params = "hashed_no=" + escape(no) + "&name=" + escape(name);
...
request.send(params);
...

Leave a Reply

Your email address will not be published. Required fields are marked *