【发布时间】:2015-05-20 06:23:41
【问题描述】:
我创建了一个网页,加载该网页时会触发一个 AJAX 请求,该请求会引入数据,然后更新该页面。
现在一切正常,除了每次加载页面时都会触发 ajax 请求,即使服务器上的内容没有更改。那么如何优化这个过程,使页面不会一次又一次地触发 ajax 请求呢?
代码过程如下
我使用正则表达式检查网址 courseInfoRegexUrl = /(http|ftp|https)://[\w]+/[\w]+/courses/course(.php|)?/gi;
如果匹配那么我调用一个带有arg的函数,函数如下:-
function getCourseData(name){
$.ajax({
url : "api/getCourseData.php",
type : "POST",
data : {name:name, token:catoken},
success : function(d){
d = JSON.parse(d);
console.log(d);
getCourseReviews(name);
},
error : function(error){
generalErrorShow("Some error ocurred. Try refreshing the page");
}
});
}
它的调用例子是getCourseData("comp1");
谢谢
【问题讨论】:
-
让您的服务器通过发送
Cache-Control标头来告诉浏览器缓存响应。