Ajax get method to generate IE Cache

Source: Internet
Author: User

Ajax get usually produces some cached data by default, if we want to disable the creation of cache, we can add a disable cache or add a random parameter after the url to solve the problem of ie cache and ajax cache.


Yesterday we encountered a problem like this:

In IE browser, the Website Cannot be logged on and exited normally.

This is weird. IE is ignored before. Testing with Firefox Google is good.

After repeated troubleshooting, we finally found the problem.

I am using the jquery get method to obtain

The cache is generated on ie. I used the ie debug check tool to check the cache and then the data is updated. It is not clear .. Later I changed it to post,

Another way is to pass a variable value to the server during get. This problem can also be solved.


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 ();

 

In addition, Baidu's Materials

--------

Yesterday, when I used prototype ajax to fetch data from the business logic, I found that the database update results can be normally displayed every time I open a new browser. If I refresh the browser, it will not change. however, after deleting the temporary IE File, refresh the file to display it normally. therefore, I conclude that IE may cache the data returned by the ajax request. I checked some information and found that the ajax get method will be cached, and some netizens proposed a solution:

1. Add the header on the server ("Cache-Control: no-cache, must-revalidate ");

2. Add anyAjaxObj. setRequestHeader ("If-Modified-Since", "0") before ajax sends a request ″);

3. Add anyAjaxObj. 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 ();

6. Replace GET with POST: Not recommended

I am still not satisfied with the above points,

1. The problem is solved by ajax on the server side, regardless of the server side;
In scenarios 2 and 3, prototype is used now. It is not a normal native ajax statement, so it should be solved under the premise of prototype;
In the case of 4 and 5, why should we add a parameter after the address? This parameter is not what my business logic needs (this method is not available yet );
In the case of "6", I can use post as a link, and sometimes I have to use a link but change it to a form. Isn't that stupid.

The solution to this problem must be based on prototype. you can see from the prototype document that there is a requestHeaders option, which is responsible for processing the request header information. The above method also mentions no cache settings in the header information, so we can just set this option. you only need to add one sentence to the prototype ajax Configuration:

The Code is as follows: Copy code

RequestHeaders: ['cache-control', 'no-cache', 'if-Modified-Since ', '0'],

Example:

The Code is as follows: Copy code

Var myAjax = new Ajax. Request ("/Personal/OperatePage. jsp ",
{
Method: "get ",
Parameters: "action = addfriend & ids =" + escape (ids ),
RequestHeaders: ['cache-control', 'no-cache', 'if-Modified-Since ', '0'],
OnComplete: function (msg ){
Alert (msg. responseText );
}
});

Jsp code

The Code is as follows: Copy code

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.