pappy asked this 3 years ago

AJAX get not refreshing the data in ASP.NET core MVC app

On a ASP.NET core app, I am using $.ajax() method to get data into a modal window when a button is clicked. This works correctly the first time the button  is clicked but if the data is updated , the subsequent calls still return the old data.

          $.ajax({
              url: $(this).data('url'),
              method: 'GET',
              success: function (data) {
                  $("#ModalDiv").html(data);
                  $("#ModalDiv").modal('show');
              },
              error: function (err) {
                  console.log(err);
              }
          });
      });
  
});

I have added a breakpoint on the controllers action method and found that its not even hitting there after the first call.



Remy Varghese 3 years ago
$.ajaxSetup({ cache: false });

To prevent caching of all future AJAX requests:

Arnold 3 years ago

This is due to browser caching. You can disable caching by setting the option cache:false

  $.ajax({
 url: $(this).data('url'),
 cache: false,
 method: 'GET',
 success: function (data) {
  $("#ModalDiv").html(data);
  $("#ModalDiv").modal('show');
 },
 error: function (err) {
  console.log(err);
 }
          });
});