相同url的ajax请求_IE缓存
2010-03-05 01:20
183 查看
项目中要实现一个邮件发送的功能,当填入邮箱后点击发送,页面不刷新,只给出一个“邮件正在发送中”的提示信息,使用ajax发送请求,并根据 ajax请求的返回值来刷新提示信息(如果返回true,提示信息刷新为“邮件已经成功发送,请注意查收”)。当再次点击发送时(邮件地址不变),提示信息又变为“邮件正在发送中”,将邮件在发送一次。在firefox下面运行一起正常,但是在ie下面就出问题了,当邮件地址不变时,第二次点击发送,邮件提示信息仍为“邮件发送成功,请注意查收”(其实是先刷新为“邮件正在发送中”,再被刷新为“邮件发送成功......”的,只不过中间的时间差太小,看不到这个过程),邮件也没有被再次发送。
感觉是缓存的问题,g了下,找到篇不错的文章,转载如下:
(原文:http://hi.baidu.com/bit_kevin/blog/item/70648cdd21e357315982dd91.html )
在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.
感觉是缓存的问题,g了下,找到篇不错的文章,转载如下:
(原文:http://hi.baidu.com/bit_kevin/blog/item/70648cdd21e357315982dd91.html )
在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.
相关文章推荐
- Ajax相同url的请求,IE缓存问题
- Ajax相同url的请求_IE缓存问…
- ajax提交相同url,重复发送请求后台,页面无更新的问题解决
- Ajax多次请求同一个URL返回相同值问题
- 利用JS面向对象+模块化封装集AJAX请求、基本数据校验、URL参数截取等功能于一身的通用工具模块
- jquery 请求ie缓存,数据不更新的问题【ajax禁用缓存解决ie数据不更新的问题】
- ajax碰到请求url过长的问题! ajax中get和post的区别
- IE8\9在进行ajax请求时url注意事项
- jquery 请求ie缓存,数据不更新的问题【ajax禁用缓存解决ie数据不更新的问题】
- Ajax在静态页面中向指定url发送json请求获取返回的json数据
- 读SDWebImage库系列(1)-如何保证同一时间请求相同URL时,只进行一次网络请求
- 关于解决JQuery发送Ajax请求后,IE缓存数据不更新的问题
- 单独将ajax请求放入js文件中URL问题
- ajax请求时,在url没错的情况下返回error问题
- ajax请求url 绝对路径与相对路径
- 利用JS面向对象+模块化封装集AJAX请求、基本数据校验、URL参数截取等功能于一身的通用工具模块
- web中 filter基于不同请求(ajax,url)过滤返回不同结果
- Ajax的url请求中为何要加随机数
- IE缓存Get方式的ajax请求
- jquery 请求ie缓存,数据不更新的问题【ajax禁用缓存解决ie数据不更新的问题】