Dakadaka
Dakadaka

Reputation: 1503

ie9 jquery ajax call first time doing well second time NOT

IE9 and jquery AJAX problem.... "first time it works well if I click on button, but second time it doesn't...I assume cache" I have jquery https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js And simple ajax call:

$('#isvalidcompany').click(function(event) {
    var image = $('#isvalidcompany_img');
    var old_state = image.attr('src');
    image.attr('src', '/images/loading.gif');
    $.getJSON('/ajax/change',
        function(data) {
            if (data['error'] != '') {
                image.attr('src', old_state);
                $('#isvalidcompany_error').html(data['error']);
            } else {
                if (data['isvalidcompany'] == 1) {
                    image.attr('src', '/icons/tick_16.png');
                } else {
                    image.attr('src', '/icons/delete_16.png');
                }
            }
        });
    return false;
});

And on all browser it is working well, except ie9 and ie8 and ie7 So if anyboday have experience on this please share :)

Upvotes: 7

Views: 4960

Answers (5)

eightynine
eightynine

Reputation: 127

What is happening is that you’re likely making a GET request to a web service for your AJAX call. Internet Explorer, in its wisdom, will automatically cache responses from GET requests while other browsers will let you decide if you’d like to cache the result or not. Once IE has successfully made a GET request, it will no longer even make that AJAX call until the cache expires on that object. check this for more details

//Disbable cache for all jQuery AJAX requests

$.ajaxSetup({ cache: false });

Upvotes: 1

JMax
JMax

Reputation: 26591

Use the cache parameter of the .ajax() method:

$.ajax({
  url: "/ajax/change",
  success: function(data){
    // your callback
  },
  dataType: JSON,
  cache: false
});

[EDIT] Anthony's solution will prevent cache from every request while my solution will prevent caching the current request... See what fits better to your needs

Upvotes: 11

Anthony Grist
Anthony Grist

Reputation: 38345

Yes, Internet Explorer caches the responses to AJAX calls to the same URL. You can use the following bit of code to get around this:

$.ajaxSetup({
    cache: false
});

That will set the cache property for all jQuery AJAX calls to false, which will result in the automatic inclusion of a timestamp parameter.

Upvotes: 6

Eben Roux
Eben Roux

Reputation: 13256

It is most probably caching. IE actually gets this right :)

You can either use the cache: false parameter or just go with a POST as opposed to a GET.

Upvotes: 0

Ghyath Serhal
Ghyath Serhal

Reputation: 7632

I experienced such error one time, I was a caching problem, so solve it, each time I send a new ajax request, I appended the url with a new random number.

Hope this helps you.

Upvotes: 2

Related Questions