Jquery Ajax请求第二次没有到达服务器

时间:2022-12-01 11:00:24

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

我正在网站上工作,我需要通过Jquery ajax调用服务来读取数据。

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.

但是,当我使用相同的URL再次调用它时,请求直接转到“成功”,而不通过Web服务。就像数据从前一个请求缓存一样,它直接返回它。

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

对于服务器端,我使用带有Entity framework 6.0的WCF Web服务和存储过程来从数据库中读取

My questions are :

我的问题是:

  1. Is this a client behavior or a server behavior?
  2. 这是客户端行为还是服务器行为?
  3. What would happen if the data changes on the server? will I still get the old data because it is cached?
  4. 如果服务器上的数据发生变化会发生什么?我还会得到旧数据,因为它是缓存的吗?
  5. if that's the case How can I prevent this behavior? because the data is constantly changing on the server
  6. 如果是这种情况我该如何防止这种行为?因为数据在服务器上不断变化

Thanks for any clarifications

谢谢你的任何澄清

2 个解决方案

#1


4  

  1. Client
  2. 客户
  3. Yes
  4. Use a cache buster, jQuery.ajax does this if you set cache to false
  5. 如果将cache设置为false,请使用缓存存取器jQuery.ajax执行此操作
$.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 () {
    }
});

#2


0  

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

当您进行Get调用时,它将被您的浏览器缓存(客户端行为)。您可以在通话中使用cache:false

$.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

将缓存设置为false只能与HEAD和GET请求一起正常工作。它的工作原理是将“_ = {timestamp}”附加到GET参数

More Info

更多信息

#1


4  

  1. Client
  2. 客户
  3. Yes
  4. Use a cache buster, jQuery.ajax does this if you set cache to false
  5. 如果将cache设置为false,请使用缓存存取器jQuery.ajax执行此操作
$.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 () {
    }
});

#2


0  

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

当您进行Get调用时,它将被您的浏览器缓存(客户端行为)。您可以在通话中使用cache:false

$.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

将缓存设置为false只能与HEAD和GET请求一起正常工作。它的工作原理是将“_ = {timestamp}”附加到GET参数

More Info

更多信息