Prevent the data in the AJAX request cache of jquery

Source: Internet
Author: User
Tags add time

Method One,

The parameter type is changed from Get to post
Add parameter cache and set to False
Add Time stamp

The code is as follows Copy Code
$.ajax ({
URL: ' Ios/index ',
Cache:false,
Type: ' Post ',
Data: {
Timestamp:new Date (). GetTime ()
params here
},
DataType: ' JSON '
). Done (function (data) {
Codes here
});

Method Two,

JQuery Load Sample code:

The code is as follows Copy Code
$ (document). Ready (function () {
$ ("#labels"). Load ("/blog/categories/labels.html");
Inserts the contents of the labels.html in the DOM element with ID #labels when the page is loaded.
});

When I update labels.html, the Load method in IE7 is still using the old labels.html, even if I press the Refresh key, it doesn't work. Fortunately jquery provides a way to prevent Ajax from using caching, and adding the following statement to the head's JavaScript file can solve the problem.

The code is as follows Copy Code

$.ajaxsetup ({
Cache:false//Turn off AJAX-appropriate caching
});

In addition, I'll introduce several ways to fix caching. Note: I have not tested the jquery load problem, these methods are for reference only


In short, get-mode transfer data is small, high processing efficiency, low security, will be cached, and post instead.

You need to be aware of using get methods:

1 for Get requests (or any that involve passing parameters to the URL), the passed parameters are processed first by the encodeURIComponent method. Example: var url = "Update.php?username=" +encodeuricomponent ( username) + "&content=" +encodeuricomponent

(content) + "&id=1";


Use POST method to note:

1. Set header Context-type to Application/x-www-form-urlencode ensure that the server knows that there are parameter variables in the entity. Usually use the setRequestHeader ("Context-type", "application/x-www-form-urlencoded;") of the XMLHttpRequest object. Cases:

Xmlhttp.setrequestheader ("Content-type", "application/x-www-form-urlencoded");
2. The parameter is a name/value one by one corresponding to the key value pairs, each pair of values separated by the & number. like Var name=abc&sex=man&age=18, pay attention to Var name=update.php?

The abc&sex=man&age=18 and the Var name=?abc&sex=man&age=18 are all wrong;
3. Parameters sent in the Send (parameter) method, example: Xmlhttp.send (name); If it is a get way, direct xmlhttp.send (NULL);

4. Server-side request parameters differentiate get and post. If the Get method is $username = $_get["username"]; If it is the POST method, then $username = $_post["username"];

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.