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
}
Comments
In my experience, the ajax call from IE7 does not pass any cookies. So, from the server’s perspective, the ajax call operates with a different session than the original HTML page request. Is there anyway to force IE to send those cookies along with the ajax post?
Needless to say, other browsers work as expected.
@Dave – As far as I’ve been able to tell, there’s no way to force this sort of behavior in IE7. I’m open to any ideas and suggestions since this is certainly not an ideal situation for cross-browser standardization.