2013-02-15 70 views
1

如果在服务器上发生更改,ajax每秒请求页面自己的url,然后检查textStatus是否为notmodified,我试图使页面重新加载。如果在服务器上修改了重新加载页面

setTimeout(function(){ 
    $.ajax({ 
    url : window.location.pathname, 
    dataType : "text", 
    ifModified : true, 
    success : function(data, textStatus) { 
     if (textStatus !== "notmodified") { 
     location.reload(); 
     } 
    } 
    }); 
}, 1000); 

然而,textStatus总是success

+0

检查您的服务器的缓存标头。 – SLaks 2013-02-15 14:41:52

+0

我使用[Mongoose](http://code.google.com/p/mongoose/)作为Web服务器。 – Theodor 2013-02-15 14:53:05

回答

1

用随机变量只是尽量避免响应的缓存,本身。另外,用!=代替!==

setTimeout(function(){ 
    $.ajax({ 
    url : window.location.pathname + "?" + (new Date()).getMilliseconds(), 
    dataType : "text", 
    ifModified : true, 
    success : function(data, textStatus) { 
     if (textStatus != "notmodified") { 
     location.reload(); 
     } 
    } 
    }); 
}, 1000); 

如果这不起作用,请尝试更换:

location.reload(); 

有了:

location.href = location.href; 

这也依赖于服务器端脚本。它还需要从服务器端发送...通过设置no-cachecontent-expires

+1

这是疯了。 JavaScript的全部重点是查看缓存版本是否仍然新鲜,如果不是,则只重新加载!如果你打算改变URL来缓存缓存,那么你不妨去掉Ajax,然后点击'location.reload()'。 – Quentin 2013-02-15 14:45:46

+0

那么你如何检查缓存?它还需要从服务器端发送...通过设置“no-cache”和“content-expires”。将在答案中更新。你有更好的吗? – 2013-02-15 14:46:30

+1

代码试图使用If-Modified-Since或ETAGS请求(使用Ajax),然后查看是否返回OK或Not Modified(将从服务器端代码发送)。设置“无缓存”与每次更改URL都会有大致相同的问题。它将永远停留在一个缓存版本中。 – Quentin 2013-02-15 14:47:35

相关问题