Use the escape() function:
var comment = 'i&you'; var url = 'index.php?comment=' + escape(comment); #=> index.php?comment=i%26you
Edit:
Missed part of jQuery, sorry. In your call to $.ajax() do the following:
$.ajax('index.php', { 'data': { 'comment': 'i&you', ... } ... });
By passing an object (or string) to the data property in the options ( documentation ) argument, you can ensure that your data is properly escaped without having to explicitly do it yourself.
source share