jQuery: the correct way to prevent caching in AJAX call

By | June 8, 2021

Recently, I was creating an application in which, I needed to make an AJAX call to fetch some data from the server-side to show on the web page however, on the first call I got the correct result and after changing the record in the database table when I was making another AJAX request from the same URL, I was getting the old records each time.

This was the code:

$.ajax({
    url: 'https://www.example.com/ajax/get_member_history',
    type: 'GET',
    dataType: 'json',
    success: function(data) {
        alert(data);
    }
});

To fixed issue, I added cache=false in my code, and everything started working as expected.

$.ajax({
    url: 'https://www.example.com/ajax/get_member_history',
    type: 'GET',
    dataType: 'json',
    cache: false,
    success: function(data) {
        alert(data);
    }
});

Hope it will help you.