OtoNoOto OtoNoOto - 2 years ago 179
jQuery Question

jQuery AJAX URL Ampersand (&) Formatting

Using the following script I am capturing date value from jQuery datepicker and passing that on to a jQuery AJAX call. Everything is working, but running into an issue with ampersands in the AJAX URL.

This is my code:

<script type="text/javascript">

dateFormat: 'yy-mm-dd',
onSelect: function (date) {

//capture date values from datepicker
var date = date;
var year = date.substr(0,4);
var month = date.substr(5,2);
var day = date.substr(8,2);
var url = "http://www.example.com?view=week" + "&year=" + year + "&month=" + month + "&date=" + day;

//ajax events load
$.ajax({url: url ,success:function(result){

//close datepicker

return false;



When I inspect the AJAX request using fiddler you can see the URL format is as follows:


Note that all the '&' characters were converted in the URL. In order for the query to work correctly I need the format to be:


Any ideas how I can prevent the format from changing in the AJAX request?

Thanks in advance!

Answer Source

Add a data element to your jQuery ajax function

  data: { year: "2014", month: "Jan", date: "15" }

jquery will add it to the URL for you.

 //ajax events load
 $.ajax({url: url, data: { year: '"'+year+'"', month: '"'+month+'"', date: '"'+day+'"'} ,success:function(result){
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download