jQuery.get无法接收服务器上文件的最新版本 [英] jQuery.get doesn't receive latest version of file on server

查看:85
本文介绍了jQuery.get无法接收服务器上文件的最新版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
防止AJAX调用的缓存

Possible Duplicate:
Prevent caching of AJAX call

我正在使用jQuery读取Web服务器上的一个很小的文本文件:

I'm using jQuery to read a tiny text file on a web server:

jQuery.get('scores.txt', function(data) {
    parseScores(data);
});

这几乎可以正常工作-但不能可靠地加载文件的最新版本.即使story.txt已更改,刷新页面也会为data返回完全相同的值.

This nearly works as it should -- but it doesn't reliably load the latest version of the file. Even if story.txt has changed, refreshing the page returns exactly the same value for data.

该服务器是标准的Mac OS Apache,未启用缓存,并且与客户端在同一台计算机上-因此从理论上讲,我与它之间没有其他缓存.

The server is standard Mac OS Apache, with no caching enabled, and it's the same machine as the client -- so in theory there are no other caches sitting between me and it.

如果我将http://127.0.0.1/scores.txt加载到浏览器中,则它将始终打开最新版本.

If I load http://127.0.0.1/scores.txt into the browser then it always opens the latest version.

查看Apache日志,对于页面无法更新的情况,jQuery GET请求永远不会到达服务器.

Looking at the Apache log, for the times the page fails to update, the jQuery GET request never reaches the server.

我怀疑我可能需要将此问题移到serverfault.com,但是有人可以看到我的JavaScript出现问题,使它以这种方式运行吗?

I have a suspicion that I might need to move this question to serverfault.com, but can anyone see a problem with my JavaScript that would make it behave in this way?

是否可以使用JavaScript/jQuery强制浏览器请求文件的新副本?

Is there any way of using JavaScript/jQuery to force the browser to request a new copy of the file?

谢谢.

推荐答案

尝试添加 cache: false 您的AJAX选项:

Try adding cache: false to your AJAX options:

jQuery.ajax({
    url: 'scores.txt',
    dataType: 'text',
    type: 'GET',
    cache: false,
    success: parseScores
});

jQuery将自动为每个请求添加一个防止缓存的时间戳.

jQuery will automatically add a cache-preventing timestamp to each request.

这篇关于jQuery.get无法接收服务器上文件的最新版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆