IE 7/8 AJAX Cache Weirdness
Alright, here's a good puzzle for you guys. I'm working on this function that fetches JSON from a PHP web services and displays the results to a div. The results seem to not display or would lead you to think that either the web service isn't outputing any data or the ajax call is wrong. Further investigation has shown that the web service IS outputing the data and the ajax call IS correct and sending the proper variables to the service.
Here's where it gets interesting...
If I press F12 and open the console, refresh the page, and then click the link that initiates the ajax call.. 开发者_StackOverflow社区wait for it... it works!
Anyone know why this happening or better yet, how to make IE7/8 STOP caching AJAX?
WOW, I really hate IE!
My code:
function getActivity(aid, callback) {
var date = new Date();
var timestamp = date.getTime();
var params = {
apiKey: apiDefaults.key,
service: 'wb_getActivity',
agencyID: $('#booking-agencyID').val(),
activityID: aid,
nocache: timestamp
};
$.ajax({
cache: false,
url: apiDefaults.url,
data: params,
dataType: 'json',
type: 'post',
success: function (json) { console.log(json); callback(json); }
});}
Put a dynamic variable in the url like the current datetime to force the browser not to cache the request and the response.
The answer to this was that I have the console.log() function going in IE w/o the debugger being open.
Another way to set Cache to false if thats not working is to change jQuerys global setting.
$.ajaxSetup({
cache: false
});
This can also help if your using the $.get or $.post extensions of $.ajax
精彩评论