简体   繁体   English

强制用户重新加载页面

[英]Force user to reload page

Background information: 背景资料:

  1. I have JavaScript that makes an AJAX request. 我有发出AJAX请求的JavaScript。
  2. I am using a version URL parameter to make sure that when a page loads it gets the latest version of the JavaScript file: /path/to/file.js?v=20160511 我正在使用版本URL参数来确保在页面加载时获取最新版本的JavaScript文件: /path/to/file.js?v=20160511

That problem I need to solve now is when I make a change to the data that is returned by the AJAX call (I assume this is considered an API break), and the JavaScript file (client). 我现在需要解决的问题是,当我更改由AJAX调用返回的数据(我认为这被认为是API中断)和JavaScript文件(客户端)时。 My first thought would be to somehow force the user to reload the page when I release a change to the API. 我的第一个想法是在发布对API的更改时以某种方式迫使用户重新加载页面。 What is a good process to make sure the client and server stay in sync? 有什么好的方法来确保客户端和服务器保持同步?

您可以使用AJAX在客户端上触发“ location.reload();”。

Based on Uğurcan 's comment and Hullu2000 's answer , I added two things to my current implementation. 基于Uğurcan的评论和Hullu2000回答 ,我在当前的实现中添加了两件事。 The total necessary items are: 总共需要的项目是:

  1. The JavaScript referenced in page has a version URL parameter to make sure that when a page loads it gets the latest version of the JavaScript file: /path/to/file.js?v=20160511 页面中引用的JavaScript具有版本URL参数,以确保在页面加载时获取最新版本的JavaScript文件: /path/to/file.js?v=20160511
  2. The API call returns a JSON response with an API version string API调用返回带有API版本字符串的JSON响应
  3. The JavaScript checks the API response version string against a local variable. JavaScript根据本地变量检查API响应版本字符串。 If they don't match, it calls window.location.reload(); 如果不匹配,则调用window.location.reload();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM