How to % encode single quotes when making jQuery AJAX call? <p>I am using $.ajax to send a SQL query to my web server. The query string contains several single quotes ('). I encountered a very messy problem on encoding single quotes.</p> <p>My code like this, please note the single quotes in query string:</p> <pre><code>var query = "select SID, age from Students where Name=\'Jason\'" + String.fromCharCode(10) + "order by age asc"; $.ajax({ url: "http://mywebserver/query", data: { env: "dbserver1", endTime: "getUTCDate()", startTime: "dateadd(hour, -336, getUTCDate())", text: query }, type: "GET", dataType: "json" }).done(function (datum) { }); </code></pre> <p>If I don't explicitly call encodeURIComponent before make AJAX call, jQuery will encode it for me, however, the single quotes are not encoded to %27 by default, so query doesn't work;</p> <p>If I pass an encoded query string to jQuery, it will encode again, that messes up the query string.</p> <p>The only solution i can imagine is, i have to overwrite the behavior how jQuery encodes URL, and replace all (') with %27. But I don't know if jQuery supports that. Does anyone have a solution for this?</p>