Analysis and Solution of Ajax ie cache Problems

Source: Internet
Author: User

Recently, I made an Ajax-related function. The goal is to generate different welcome languages based on different login statuses of users, as shown below:

When the user logs in, it will show: Hello, come again!

If the user does not log on, it will show that you have not logged on!

 

After I wrote Ajax, I found a fatal problem:

Everything works in Firefox, but it's okay to open ie for the first time. If you refresh it again, Chinese characters will become garbled!

 

I checked it carefully and did not get any results. Later, with the help of laruence, I found it was an IE cache problem.

OK. The problem is as follows:

 

Principle:

Every time Firefox requests, it will return to the server again to obtain the latest data, but IE will be different, it will cache
Only the first request will actually read the data from the server, so that the Ajax data will not be updated over time ....

I opened the cache folder of IE and found a file named after the Ajax request address. When I opened it, I realized why it was garbled, because the file is stored in ANSI. Maybe IE will not use the GBK encoding format to read this file, but will use Unicode

 

Solution (collected from the Internet ):

1,AddHeader ("cache-control: No-cache, must-revalidate"); or use the following combination to make it better:

 

Header ( " Expires: sat, 1 Jan 2005 00:00:00 GMT " );
Header ( " Last-modified: " . Gmdate ( " D, D m y h: I: S " ) . " GMT " );
Header ( " Cache-control: No-cache, must-revalidate " );
Header ( " Pragma: No-Cache " );

 

 

2 , in Ajax Add XMLHttpRequest. setRequestHeader ("If-modified-since", "0");

3 , in Ajax Add XMLHttpRequest. setRequestHeader ("cache-control", "No-Cache");

4 , in Ajax URL Add "? Fresh = "+ math. random (); // Of course, here the parameter fresh Any

5 , the fourth method is similar to the third method, in URL Add "? Timestamp = "+ new date (). gettime (); // This method is recommended. I use this method and I personally think it is more convenient.

6,UsePostSubstitutionGet: Not recommended

 

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.