A quick solution to the Ajax caching problem under IE

  • 2020-03-30 01:17:39
  • OfStack

After a lot of confusion, the program used jquery's load method to make the request. It was strange that the request could not be sent the second time. Baidu a, who knows load is to use the get way to request, so IE browser on

It's cached. There are a lot of solutions on the Internet, a lot of them, and here is what I think is a more comprehensive solution. It is mainly divided into client side solution and server side solution.

1. Client side solution
IE access policy: Internet options - browsing history - Settings - Internet temporary file options changed to every time you visit a web page

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 a new Date ();

2: the XMLHttpRequest request before with XMLHttpRequest. SetRequestHeader (" the if-modified-since ", "0")

In general, XMLHttpRequest here is not used directly
You should be able to find this code
XXXXX. Send (YYYYYY);
So, let's change this into
XXXXX. SetRequestHeader (" the if-modified-since ", "0");
XXXXX. Send (YYYYYY);

Practice has proved that both methods are very effective.
1. Add a header(" cache-control: no-cache, must-revalidate") on the server side;
2, in front of the ajax request with the xmlHttpRequest. SetRequestHeader (" the if-modified-since ", "0");
3, before the ajax request with the xmlHttpRequest. SetRequestHeader (" cache-control ", "no - Cache");
4. Add "" after the Ajax URL parameter? Fresh = "+ Math. The random (); // of course, the parameter fresh can be arbitrarily selected here
5. The fourth method is similar to the third, adding "after the URL parameter "? Timestamp = "+ new Date (). GetTime (); // this method is recommended
POST instead of GET: not recommended


2. Server-side solution:

Take Struts2 as an example:
Struts2 Server side usage

Xml code


<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

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 articles: