Escape all special characters in a string that is sent by jquery ajax

For those who will find this question: Do not use escape method it has been removed from the Web Use encodeURIComponent() or encodeURI() instead
encodeURIComponent()
encodeURI()


Why not use escape?

escape(text);

https://developer.mozilla.org/en/DOM/window.escape

EDIT!!!!

As mentioned in comments, this is deprecated.

The deprecated escape() method computes a new string in which certain characters have been replaced by a hexadecimal escape sequence. Use encodeURI or encodeURIComponent instead.

Instead use one of the following:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURI

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent


I was having the same problem, and to solve it, I change the way I was making the ajax call.

I had something like

var datatosend = "Hello+World";

$.ajax({
    "type": "POST", 
    "data": "info=" + datatosend 

And it send on the post info=Hello World, replacing the character + with a blank space.

So I change it into a correct json string

$.ajax({
    "type": "POST", 
    "data": {"info":datatosend}, 

and now it works. info=Hello+World

Tags:

Text

Jquery