ajax2-Troubleshooting Cache Issues-php (28)

Source: Internet
Author: User

V.Ajax Quick Start

Ajax Code:

PHP Program:

Vi. Sending of data

Ajax GET request, the data to be sent needs to be appended to the URL

Ajax POST request, the data to be sent needs to be placed in the parameters of the Send method

Ajax Code:

PHP Code:

Deliberately write the URL of the request is wrong, we found that the use of a request for a nonexistent URL, then the Ajax object status code will eventually be equal to 4, and execute the corresponding statement, and we hope that if the request is an error page, then we should not execute the corresponding statement

Make some corresponding changes.

Using Ajax to calculate two-digit and

Vii. Resolving cache issues

The above questions:

Change the addition of PHP to subtraction

If the previous request parameter is used, the discovery is still additive, and if the new request parameter is the subtraction operation.

Workaround 1: Random number

Math.random ();

Generate a JS random number after the URL, so that the URL of the request is unique, so each request back is the latest data

However, we know that this approach does not fundamentally solve the caching problem, it just produces a lot of cache files

One more thing: random numbers don't guarantee that this URL is absolutely unique.

Workaround 2:

Time

New Date (). GetTime (); Get millisecond Timestamp

The above method attaches the timestamp directly after the URL, so that the current URL is unique, this method from the execution, it is always unique

However, it will still generate a large number of cache files under temporary files.

Workaround 3, set the request header

setRequestHeader("if-modified-since", "0");

Principle: The use of Ajax objects before sending an HTTP request, set the HTTP request header information, indicating that the current resource to be requested last modification time is "0", after the server has taken this time, will be compared with the last modification time of this file on the server, if different, then return the latest execution results.

In this way, there is always only one cache file at the end.

Workaround 4. Set the response header

Header ("Cache-control:no-cache, must-revalidate");

Using the header function of PHP to write data to the response header, write to tell the client: do not cache the results of this time.

This approach can fundamentally solve the caching problem without generating any cache files.

Examples:

Verify that the user name can be used

ajax2-Troubleshooting Cache Issues-php (28)

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.