July 2008
Mon Tue Wed Thu Fri Sat Sun
« Feb   Aug »
 123456
78910111213
14151617181920
21222324252627
28293031  

Day July 28, 2008

Missing cookies in jQuery AJAX callbacks?

For my current project, we’re making use of a large number of jQuery AJAX callbacks, as well as cookies, to deliver some rich functionality to the users of a web site. Unfortunately this created a little bit of an issue when trying get this to work in IE7.

Here’s what I originally attempted:


function do_something() {
  $.ajax({
    url: 'http://example.com/AjaxRocks',
    type: 'POST',
    dataype: 'json',
    success: success_response
  });
}
function success_response(json) {
  var cookie_data = load_data_from_cookie(); // the implementation of this is not important
  // unfortunately, cookie_data is null so this doesn't really give us the important cookie_data
}

For some reason, the full contents of document.cookie are not available from inside the success_response function call. The solution? Load the cookie’d data before making the AJAX call:


function do_something() {
  var cookie_data = load_data_from_cookie();
  $.ajax({
    url: 'http://example.com/AjaxRocks',
    type: 'POST',
    dataype: 'json',
    success: function(json) {
      success_response(json, cookie_data);
    }
  });
}
function success_response(json, cookie_data) {
  // do something successful
}

This site is protected with Urban Giraffe's plugin 'HTML Purified' and Edward Z. Yang's Powered by HTML Purifier. 401 items have been purified.