AWG Blogs

Sunday, August 9, 2009

Use encodeURIComponent in AJAX

In the XMLHttpRequest "send" method it's best to encode the parameters using the Javascript function encodeURIComponent as opposed to escape or encodeURI. For details why, see
http://xkr.us/articles/javascript/encode-compare/ .

I learned this by researching why the solution at http://www.captain.at/howto-ajax-form-post-request.php did not work for me. For example, if I use escape, the '+' symbol converts to a space in the database; and if I use encodeURI, any '&' will cause all subsequent characters to not make it into the database -- because those characters aren't converted with the other two Javascript methods.