Y2theZ
Y2theZ

Reputation: 10412

Jquery Ajax request not reaching the server the second time

I am working on a website, I need to call a service via Jquery ajax to read data.

I am doing the following:

$.ajax({
    type: "GET",
    async: "true",
    dataType: 'json',
    url: urlString,
    beforeSend : function (){

    },
    success: function (data) {
        LoadData(data);
    },
    failure: function (request, status, error) {
        alert("Request failed. Please try again later.");
    },
    complete: function () {
    }
});

The request is working properly.

However when I call it again with the same URL the request goes directly to the "success" without passing through the webservice. Its like the data was cached from the previous request and it is returning it directly.

For the server side, I am using a WCF webservice with Entity framework 6.0 and stored procedures to read from the database

My questions are :

  1. Is this a client behavior or a server behavior?
  2. What would happen if the data changes on the server? will I still get the old data because it is cached?
  3. if that's the case How can I prevent this behavior? because the data is constantly changing on the server

Thanks for any clarifications

Upvotes: 2

Views: 2589

Answers (2)

Adil Shaikh
Adil Shaikh

Reputation: 44740

As you are making a Get call, it will get cached by your browser(client behavior). You can use cache: false in your call

$.ajax({
    type: "GET",
    async: "true",
    cache: false,   <---------

Setting cache to false will only work correctly with HEAD and GET requests. It works by appending "_={timestamp}" to the GET parameters

More Info

Upvotes: 0

Musa
Musa

Reputation: 97717

  1. Client
  2. Yes
  3. Use a cache buster, jQuery.ajax does this if you set cache to false
$.ajax({
    type: "GET",
    cache: false,
    async: "true",
    dataType: 'json',
    url: urlString,
    beforeSend : function (){

    },
    success: function (data) {
        LoadData(data);
    },
    failure: function (request, status, error) {
        alert("Request failed. Please try again later.");
    },
    complete: function () {
    }
});

Upvotes: 4

Related Questions