What we do in life echoes in eternity!
sys7em
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:

$.ajax({
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.

Comments:

Write a comment

*Name
E-mail:
*Security code:
*1+2 = (write in words ;)
*Message

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 "