What we do in life echoes in eternity!
Home About Me Diary

ajax - How to escape & and % in a POST request in jQuery?

posted on: 16:29, May 28th , 2014

I have input element like that

<input type="checkbox" value="rock&amp;pop" />

When i try sending it through an ajax request:

type: "POST",
url: "/add.php",
data: "id="+id+"&value="+value+"&type="+type,
error: function(){alert(error...');}

Nothing gets sent and the script breaks.

the solution:
You can set your data option as an object and let jQuery do the encoding, like this:

data: { id: id, value: value, type: type },

Now specil chars are escaped like they should.


Write a comment

*Security code:
*1+2 = (write in words ;)

All materials on this site are licensed under the following license: "Steal every piece of information you can get your hands on and run as fast as you can "