Quick Solution to Ajax cache problems in IE (get method)

Source: Internet
Author: User

After a long time, the program uses the jquery load method for the request. It is strange that the second request cannot be sent. Baidu, but I know that load is requested in get mode, so IE

It is cached. I searched for a lot of solutions on the Internet. I think the following is a comprehensive solution. It mainly includes client and server solutions.

1. Client Solution
Internet Access Policy: Internet Options-browsing history-settings-Temporary Internet Files can be changed

1: Add a random function after the page of the AJAX request. 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: Before XMLHttpRequest sends a request, add XMLHttpRequest. setRequestHeader ("If-Modified-Since", "0 ")

Generally, XMLHttpRequest won't be used directly.
You should be able to find such code
XXXXX. send (YYYYYY );
Then, convert it
XXXXX. setRequestHeader ("If-Modified-Since", "0 ");
XXXXX. send (YYYYYY );

Practice has proved that both methods are very effective.
1. Add the header ("Cache-Control: no-cache, must-revalidate") on the server ");
2. Add xmlHttpRequest. setRequestHeader ("If-Modified-Since", "0") before ajax sends a request ");
3. Add xmlHttpRequest. setRequestHeader ("Cache-Control", "no-cache") before ajax sends a request ");
4. Add "? Fresh = "+ Math. random (); // Of course, the fresh parameter can be any one here
5. The fourth method is similar to the third method. Add "? Timestamp = "+ new Date (). getTime (); // This method is recommended.
6. Replace GET with POST: Not recommended


2. server-side solutions:

Take Struts2 as an example:
Struts2 Server side usage

Xml Code
Copy codeThe Code is 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 codeThe Code is as follows:
Public class CachingHeaderInterceptor extends actinterceptor {

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.