Fast solution to Ajax caching problem in IE (get way) _javascript skills

Source: Internet
Author: User

After a while, the program uses the jquery load method to request, and it's strange why the request cannot be sent for the second time. Baidu A, who knows the load is to use get way to request, so IE browser to

It is cached. Online Search a lot of solutions, a bunch, below is I think more comprehensive solution. Mainly divided into client solutions and service-side solutions.

1. Client Solutions
IE Access policy: Internet Options--browsing history--setting--The Internet temporary files option is changed to every time you visit a Web page

1: After the Ajax requested page, add a random function, we can use the random time function

Add T=math.random () after the URL sent by javascript
For example: url+ "&" + "t=" +math.random (); or new Date ();

2: Add Xmlhttprequest.setrequestheader ("If-modified-since", "0") before XMLHttpRequest send the request

In general, the XMLHttpRequest here will not be used directly
You should be able to find this code.
Xxxxx.send (YYYYYY);
So, just turn it into
Xxxxx.setrequestheader ("If-modified-since", "0");
Xxxxx.send (YYYYYY);

Practice has proved that both methods are very effective.
1, in the service End plus header ("Cache-control:no-cache, must-revalidate");
2, in the AJAX send the request before adding Xmlhttprequest.setrequestheader ("if-modified-since", "0");
3, in the AJAX send the request before adding Xmlhttprequest.setrequestheader ("Cache-control", "No-cache");
4, after the Ajax URL parameters plus "? fresh=" + math.random (); Of course, the parameter fresh can be arbitrarily taken.
5, the fourth method is similar to the third, after the URL parameter plus "? timestamp=" + New Date (). GetTime (); It is recommended to use this method
6. Replace get with post: not recommended


2. Server-Side solutions:

Take Struts2 as an example:
Struts2 Server End Usage

XML code

Copy Code code as follows:

<package name= "Json-nocache" extends= "Json-default" >
<interceptors>
<interceptor name= "Cachingheadersinterceptor" class= "Com.ssa.pct.web.interceptor.CachingHeaderInterceptor"/ >
<interceptor-stack name= "Defaultsecuritystack" >
<interceptor-ref name= "Defaultstack"/>
<interceptor-ref name= "Cachingheadersinterceptor"/>
</interceptor-stack>
</interceptors>

<default-interceptor-ref name= "Defaultsecuritystack"/>
</package>

Java code
Copy Code code as follows:

public class Cachingheaderinterceptor extends Abstractinterceptor {

Private static final long serialversionuid = 1L;

Public String intercept (actioninvocation invocation) throws Exception {
Actioncontext context = Invocation.getinvocationcontext ();
HttpServletResponse response = (httpservletresponse) context.get (strutsstatics.http_response);
if (response!= null) {
Response.setheader ("Cache-control", "No-cache");
Response.setheader ("Pragma", "No-cache");
Response.setheader ("Expires", "-1");
}
return Invocation.invoke ();
}

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.