Home >Web Front-end >JS Tutorial >Quick solution to Ajax caching problem under IE (get method)_javascript skills

Quick solution to Ajax caching problem under IE (get method)_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:04:47962browse

After struggling for a long time, the program uses jquery’s load method to make requests. It’s strange why the request cannot be sent the second time. Baidu did some research, but I didn’t know that load is requested using the get method, so IE browser does not respond to

It's cached. I searched a lot of solutions online, and here is what I think is a more comprehensive solution. Mainly divided into client-side solution and server-side solution.

1. Client solution
IE access policy: Internet Options--Browsing History--Settings--Change the option of temporary Internet files to every visit It can also be used on web pages

1: Add a random function after the AJAX requested page, 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 sends the request

Generally, the XMLHttpRequest here will not be used directly
You should be able to find code like this
XXXXX.send(YYYYYY);
Then, turn it into
XXXXX.setRequestHeader ("If-Modified-Since","0");
XXXXX.send(YYYYYY);

Practice has proven that both methods are very effective.
1. Add header("Cache-Control: no-cache, must-revalidate") on the server;
2. Add xmlHttpRequest.setRequestHeader("If -Modified-Since","0");
3. Add xmlHttpRequest.setRequestHeader("Cache-Control","no-cache"); before sending a request with ajax;
4. URL parameters in Ajax Add "?fresh=" Math.random(); //Of course the fresh parameter here can be chosen arbitrarily
5. The fourth method is similar to the third method, add "?timestamp=" after the URL parameter new Date().getTime(); //This method is recommended
6. Use POST instead of GET: Not recommended


2. Server-side solution:

Take Struts2 as an example:
Struts2 Server-side usage

Xml code

Copy code The code is as follows:







< /interceptor-stack>





Java code
Copy code The code is 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();
}

}

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn