AJAX POST请求被缓存

时间:2014-10-01 07:06:44

标签: javascript php ajax post http-caching

在我的网络应用程序中,我向网址/navigate.php发送了一个POST请求。它的工作方式应该如此。

问题是,这个Web应用程序也应该脱机工作。我将在由于连接问题而无法完成请求时显示通知,并且用户可以在问题解决后再次同步。

当我断开我的互联网连接以进行调试时,我发现每次请求仍然返回200状态代码。

我错误的是,浏览器不应该缓存POST请求吗?

在Stack Overflow上搜索后,我尝试了这里写的解决方案。

我在网址中添加了一个缓存区(new Date().getTime()),但没有任何变化。请求仍然以200返回。

我尝试从服务器(PHP / Ubuntu)发送以下标头:

header("Expires: Sat, 01 Jan 2005 00:00:00 GMT");
header("Last-Modified: ".gmdate( "D, d M Y H:i:s")."GMT");
header("Cache-Control: no-cache, no-store");
header("Pragma: no-cache");

我没有在AJAX中使用jQuery(因为我只需要将它用于AJAX,没有别的),否则我会使用它的cache选项,并将其设置为{{1 }}。但我想它也会做同样的事情,将缓存区域附加到网址。

我使用以下代码发送请求:

false

在网络日志(Firefox)上,这是firebug显示的标题

请求标题:

define([],function(){

var a=[

    function(){return new XMLHttpRequest()},
    function(){return new ActiveXObject("Msxml2.XMLHTTP")},
    function(){return new ActiveXObject("Msxml3.XMLHTTP")},
    function(){return new ActiveXObject("Microsoft.XMLHTTP")}

];

    var o=function(){

        var r=false;

        for(var i=0;i<a.length;i++) {

            try{

                r=a[i]();

            } catch(e) {

                continue;

            }

            break;

        }

        return r;

    };

    var verifyParam = function(param) {
        if(typeof param === "undefined" || param === null) {
            return false;
        } else {
            return true;
        }
    };

    var checkParam = function(param,defaultValue) {
        if(!verifyParam(param)) {
            return defaultValue;
        } else {
            return param;
        }
    };

    var generateCacheBust = function() {
        return (new Date().getTime());
    };

    var request = function(url,method,dataInPost,initCallback,callback,error) {

        var req = o();

        if(!req) return false;

        initCallback = checkParam(initCallback,function(){});

        callback = checkParam(callback,function(){});

        error = checkParam(error,function(){});

        initCallback(req);

        req.open(method,url,true);

        if(dataInPost) {

            req.setRequestHeader('Content-type','application/x-www-form-urlencoded');

        }

        req.onreadystatechange = function() {

            if(req.readyState!=4) {

                return;

            }

            try {

                if(req.status!=200 && req.status!=304) {

                    error(req.status);

                    return;

                } else {

                    callback(req);

                }

            } catch (e) {

                error(req.status);

                return;

            }

        }

        if(req.readyState == 4) return;

        try {

            req.send(dataInPost);

        } catch (e) {

            error(req.status);

            return;

        }

    };

    var dataToString = function(data) {

        var string = '';

        for(var key in data) {

            string += (encodeURIComponent(key)+'='+encodeURIComponent(data[key])+'&');

        }

        return string.substring(0,string.length-1);

    }

    var formattedResponse = function(req,type) {

        var responseData = req.responseText;

        if(type=="json") {

            return JSON.parse(responseData);

        } else {

            return responseData;

        }

    }

    var get = function(params) {

        if(!verifyParam(params.url)) { return false; }

        params.data = checkParam(params.data,{});

        params.responseType = checkParam(params.responseType,'text');

        params.init = checkParam(params.init,function(){});

        params.success = checkParam(params.success,function(){});

        params.error = checkParam(params.error,function(){});

        params.cache = checkParam(params.cache,true);

        if(!params.cache) {params.data.cacheBust = generateCacheBust();}

        request(params.url+'?'+dataToString(params.data),"GET",false,params.init,function(req){

            params.success(formattedResponse(req,params.responseType));

        },params.error);

    };

    var post = function(params) {

        if(!verifyParam(params.url)) { return false; }

        params.data = checkParam(params.data,{});

        params.responseType = checkParam(params.responseType,'text');

        params.init = checkParam(params.init,function(){});

        params.success = checkParam(params.success,function(){});

        params.error = checkParam(params.error,function(){});

        params.cache = checkParam(params.cache,true);

        if(!params.cache) {params.url += "?" + "cacheBust=" + generateCacheBust();}

        request(params.url,"POST",dataToString(params.data),params.init,function(req){

            params.success(formattedResponse(req,params.responseType));

        },params.error);

    };

    return {

        get:get,

        post:post

    };

});

响应标题:

POST /explorer/ajax/navigate.php?cacheBust=1412147821832 HTTP/1.1
Host: genortal.com
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:31.0) Gecko/20100101 Firefox/31.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Referer: http://genortal.com/dashboard.php
Content-Length: 12
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache

以下是我断开互联网连接时收到的标题:

请求标题:

HTTP/1.1 200 OK
Date: Wed, 01 Oct 2014 07:17:01 GMT
Server: Apache/2.4.7 (Ubuntu)
X-Powered-By: PHP/5.5.9-1ubuntu4.3
Expires: Sat, 01 Jan 2005 00:00:00 GMT
Cache-Control: no-cache, no-store
Pragma: no-cache
Last-Modified: Wed, 01 Oct 2014 07:17:02GMT
Content-Length: 744
Keep-Alive: timeout=5, max=79
Connection: Keep-Alive
Content-Type: application/json

响应标题:

POST /explorer/ajax/navigate.php?cacheBust=1412148166275 HTTP/1.1
Host: genortal.com
Connection: keep-alive
Content-Length: 12
Cache-Control: no-cache
Pragma: no-cache
Origin: http://genortal.com
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36
Content-type: application/x-www-form-urlencoded
Accept: */*
Referer: http://genortal.com/dashboard.php
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8

服务器端代码:

HTTP/1.1 200 OK
Date: Wed, 01 Oct 2014 07:22:46 GMT
Server: Apache/2.4.7 (Ubuntu)
X-Powered-By: PHP/5.5.9-1ubuntu4.3
Expires: Sat, 01 Jan 2005 00:00:00 GMT
Cache-Control: no-cache, no-store
Pragma: no-cache
Last-Modified: Wed, 01 Oct 2014 07:22:47GMT
Content-Length: 117
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: application/json

任何人都可以快速帮助解决这里发生的事情吗?这是HTTP缓存的工作吗?

2 个答案:

答案 0 :(得分:3)

您应该尝试使用缓存清除来实现此目的。我的意思是传递一个额外的参数&amp;它的变量与您的URL类似

www.mydomain.com/navigate.php; //url without cache bust parameter
myRand=parseInt(Math.random()*99999999); 
www.mydomain.com/navigate.php?rand=54321 //url with cache bust parameter

因此,在上面的缓存中,您的服务器会将其理解为新请求。

答案 1 :(得分:1)

问题不在于代码,浏览器或HTTP缓存。问题出在我的操作系统上。即使我强行断开连接,连接也没有断开连接。我解决了它重启我的桌面。

为每个人浪费一点时间而道歉!感谢。

相关问题