Ajax中请求被缓存的问题
2010-01-06 14:49
501 查看
今天搞了我三四个小时的问题,终于找到解决方案了。哈哈。
在Ajax开发中,会遇到浏览器缓存内容的问题,比如,某个元素注册了鼠标事件(onmouseover),事件触发后会通过xmlhttprequest到服务器获取内容,在不刷新页面的情况下,浏览器会缓存第一次请求的内容,服务端更新后浏览器仍然显示第一次的内容。
通常,这种请求都是xmlhttprequest发起的GET请求。根据 HTTP 规范,GET 用于信息获取,而且应该是幂等的。也就是说,当使用相同的URL重复GET请求会返回预期的相同结果时,GET方法才是适用的。当对一个请求有副作用的时候(例如,提交数据注册新用户时),应该使用POST请求而不是GET。所以浏览器会对GET请求做缓存处理。
解决办法:
一. GET请求URL后附加字串,让服务器认为不是相同的请求。
例 “http://www.example.com/index.php?class=aitcle&page=5&_t=” + new Date().getTime()
二. 在ajax发送请求前加上 xmlHttpRequest.setRequestHeader(”If-Modified-Since”,”0″)
三. 在ajax发送请求前加上 xmlHttpRequest.setRequestHeader(”Cache-Control”,”no-cache”);
四. 服务端响应请求时加 header(”Cache-Control: no-cache, must-revalidate”); (PHP)
五. 使用POST代替GET,浏览器不会对POST做缓存
附录:
RFC1945中的英文原文如下:
8.1 GET
The GET method means retrieve whatever information (in the form of an
entity) is identified by the Request-URI. If the Request-URI refers
to a data-producing process, it is the produced data which shall be
returned as the entity in the response and not the source text of the
process, unless that text happens to be the output of the process.
The semantics of the GET method changes to a “conditional GET” if the
request message includes an If-Modified-Since header field. A
conditional GET method requests that the identified resource be
transferred only if it has been modified since the date given by the
If-Modified-Since header, as described in Section 10.9. The
conditional GET method is intended to reduce network usage by
allowing cached entities to be refreshed without requiring multiple
requests or transferring unnecessary data.
Posted in 2009年08月24日 ¬ 8:27 下午
在Ajax开发中,会遇到浏览器缓存内容的问题,比如,某个元素注册了鼠标事件(onmouseover),事件触发后会通过xmlhttprequest到服务器获取内容,在不刷新页面的情况下,浏览器会缓存第一次请求的内容,服务端更新后浏览器仍然显示第一次的内容。
通常,这种请求都是xmlhttprequest发起的GET请求。根据 HTTP 规范,GET 用于信息获取,而且应该是幂等的。也就是说,当使用相同的URL重复GET请求会返回预期的相同结果时,GET方法才是适用的。当对一个请求有副作用的时候(例如,提交数据注册新用户时),应该使用POST请求而不是GET。所以浏览器会对GET请求做缓存处理。
解决办法:
一. GET请求URL后附加字串,让服务器认为不是相同的请求。
例 “http://www.example.com/index.php?class=aitcle&page=5&_t=” + new Date().getTime()
二. 在ajax发送请求前加上 xmlHttpRequest.setRequestHeader(”If-Modified-Since”,”0″)
三. 在ajax发送请求前加上 xmlHttpRequest.setRequestHeader(”Cache-Control”,”no-cache”);
四. 服务端响应请求时加 header(”Cache-Control: no-cache, must-revalidate”); (PHP)
五. 使用POST代替GET,浏览器不会对POST做缓存
附录:
RFC1945中的英文原文如下:
8.1 GET
The GET method means retrieve whatever information (in the form of an
entity) is identified by the Request-URI. If the Request-URI refers
to a data-producing process, it is the produced data which shall be
returned as the entity in the response and not the source text of the
process, unless that text happens to be the output of the process.
The semantics of the GET method changes to a “conditional GET” if the
request message includes an If-Modified-Since header field. A
conditional GET method requests that the identified resource be
transferred only if it has been modified since the date given by the
If-Modified-Since header, as described in Section 10.9. The
conditional GET method is intended to reduce network usage by
allowing cached entities to be refreshed without requiring multiple
requests or transferring unnecessary data.
Posted in 2009年08月24日 ¬ 8:27 下午
相关文章推荐
- Ajax请求缓存问题、中文乱码问题、跨域问题
- ajax请求在ie8下缓存问题
- 关于ajax请求在IE6,9下的缓存问题
- IE浏览器缓存Ajax请求之后不更新数据问题
- JQUERY的AJAX请求缓存里的数据问题处理
- AJAX 缓存请求结果的问题
- IE浏览器下使用AJAX登陆接口请求缓存与登陆不了的问题解决
- jquery 请求ie缓存,数据不更新的问题【ajax禁用缓存解决ie数据不更新的问题】
- 在ie中关于ajax请求获得数据缓存问题的解决办法
- ajax请求时读缓存的问题
- jquery 请求ie缓存,数据不更新的问题【ajax禁用缓存解决ie数据不更新的问题】
- IE下ajax请求的缓存问题
- JQUERY的AJAX请求缓存里的数据问题处理
- IE浏览器下AJAX GET请求自动缓存带来的问题
- 如何解决Ajax请求结果的缓存问题说明
- jquery 请求ie缓存,数据不更新的问题【ajax禁用缓存解决ie数据不更新的问题】
- ajax请求json文件的时候缓存问题的两种解决方法
- jquery 请求ie缓存,数据不更新的问题【ajax禁用缓存解决ie数据不更新的问题】
- IE中ajax+jsp登录界面,由于缓存导致jsp过滤器与ajax请求被拦截的问题和解决
- IE中ajax+jsp登录界面,由于缓存导致jsp过滤器与ajax请求被拦截的问题和解决