douxie2023 2015-07-13 10:34
浏览 112
已采纳

如何防止浏览器/服务器端的数据缓存?

I'm primarily an Electrical hardware engineer with a very little knowledge in php, javascript and similar languages.

I'm building a system that will track my device in a real time and visually display current GPS position of it on a Google map. My device is able to properly send data about its position (latitude and longitude) as often as I want. GPS position data is sent using a HTTP communication protocol (GET requests) and is stored on a server side in a .txt file called gps.txt.

php script extracts data from that gps.txt file and displays them on a Google map.

Here is the script written in php (I applied my own API key for properly use Google maps).

This script runs on some free web hosting platform. Take a look here.

Now, the trouble comes when I call the script from my web browser. Namely, although a gps.txt data is properly refreshed with a fresh data, these new position markers are NOT displayed on the map. It seems that either my web browser or my web server are cashing some data and don't allow the script to run over a fresh gps.txt file.

I suppose that, for many of you web software guys out there, this is a trivial problem.

I also tried to prevent data cashing on my browser by including

header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past

in my script but with NO success.

Would you, please, give me any suggestion on how to overcome this situation and always have the latest GPS position displayed on my map?

Thank you very much for your time and effort to help me.

Sincerely,

Bojan

  • 写回答

1条回答 默认 最新

  • dqqpf32897 2015-07-13 10:52
    关注

    This disables the cache for all jQuery requests:

    $.ajaxSetup ({
        // Disable caching of AJAX responses
        cache: false
    });
    

    You can paste that into you initialize function and see if it works!

    Another handcrafted solution would be adding an increasing number to the file URL each time you fetch the text file, for example gps.txt?1, then gps.txt?2, and so on: as far as the browser knows there a different URL so they (kind of) get cached separately.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)