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
}

Menu