Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ie9 jquery ajax call first time doing well second time NOT

Tags:

jquery

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 :)

like image 504
Dakadaka Avatar asked Oct 21 '11 08:10

Dakadaka


2 Answers

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

like image 177
JMax Avatar answered Nov 16 '22 22:11

JMax


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.

like image 6
Anthony Grist Avatar answered Nov 16 '22 22:11

Anthony Grist