我怎么不缓存AJAX POST在移动Safari浏览器的应用程序?缓存、应用程序、浏览器、我怎么

2023-09-10 17:51:47 作者:男娼起义丶

由于这个职位,我是能够解决的问题,即移动Safari浏览器会缓存AJAX POST请求。添加标题:{的Cache-Control':'无缓存'}似乎做的伎俩我在移动Safari网页

然而,当我进入我的网站,通过移动版Safari Web应用程序的AJAX请求仍缓存。我一直没能找到一个解决方案,这样想我会在这里发表。除了增加上述的头,我也试着加入缓存:假,以及把网址:'?/ ajax_url V ='+时间。似乎没有任何工作。

为什么在移动Safari浏览器与web应用程序的不同的行为?我该如何解决此问题?

编辑:

忘记了我的code。在这里,它是:

 函数send_ajax(my_data,刷新){
    变种现在=新的日期();
        变种N = now.getTime();
    $阿贾克斯({
      网址:/ ajax_page时间=+ N,
      键入:POST,
      标题:{缓存控制:无缓存},
      数据:my_data,
      数据类型:JSON
      })
      .fail(功能(jqXHR,textStatus,errorThrown){
      })
      .done(功能(数据){//刷新页面后,我们得到的结果
        如果(刷新=='真'){
            VAR pathArray = window.location.pathname.split('/');
            window.location.href ='/'+ pathArray [1];

        }
      });

    }

 send_ajax({'my_checkbox:$('#my_checkbox)是(:选中')},'真');
 

解决方案

一个解决方案,始终工作是一个参数添加到具有提出要求(时间戳本质上)时间的要求。这使得每个请求的服务器工作在一个独特的雪花。

它类似于把你的网站的版本到所有的名字你的脚本/ CSS文件,使用户的来到您的网站的更新后,必须下载新文件的念头。

Thanks to this post I was able to solve the issue where mobile safari will cache ajax POST requests. Adding "headers: {'Cache-Control': 'no-cache'}" seemed to do the trick for my page in mobile safari.

苹果自带浏览器无法打开网页 原来罪魁祸首竟是它

However, when I access my website via the mobile safari webapp the ajax requests are still cached. I haven't been able to find a solution so thought I'd post here. In addition to adding the header mentioned above, I've also tried adding "cache: false," as well as putting "url: '/ajax_url?v='+time". Nothing seems to work.

Why the different behavior in mobile safari vs. the webapp? How do I resolve this?

EDIT:

Forgot my code. Here it is:

function send_ajax(my_data,refresh){ 
    var now = new Date();
        var n = now.getTime();
    $.ajax({  
      url: "/ajax_page?time=" + n,
      type: "POST",
      headers: {"cache-control": "no-cache"},
      data: my_data,
      dataType: 'json'
      })
      .fail( function (jqXHR, textStatus, errorThrown){
      })
      .done(function(data){ // refresh the page after we get the results
        if(refresh=='true'){
            var pathArray = window.location.pathname.split('/');
            window.location.href = '/' + pathArray[1];

        }
      });         

    }

 send_ajax({'my_checkbox': $('#my_checkbox').is(':checked') },'true');  

解决方案

One solution that always works is to add a parameter to the request that has the time the request was made (timestamp essentially). This makes every request a unique snowflake that the server has to work on.

Its similar to the idea of putting the version of your website into the name of all your script/css files so that users that come to your site after an update have to download the new files.