Jquery encapsulates three layers of ajax:
Layer 1: $. ajax, Layer 2:. load $. get, $. post, Layer 3: $. getScript, $. getJson
First, let's learn how to use. load:
Format: load (url [, data] [, callback])
The simplest get method to get text content
| The code is as follows: |
Copy code |
|
$ ('# Ajax'). click (function (){
Upload ('character box'character .load('ddd.html '); // load the content of ddd.html to the element of id = box
});
$ ('# Ajax'). click (function (){
(('{Box'{.load('ddd.html. URL'); // load the content of class = url in ddd.html to the element of id = box.
}); |
Interaction with the server segment script
| The code is as follows: |
Copy code |
|
$ ('# Ajax'). click (function (){
$ ('# Box'). load ('test. php? Url = aitiblog '); // php file submitted to test in get mode
}); |
Complex interaction with servers:
| The code is as follows: |
Copy code |
|
$ ('# Ajax'). click (function (){
$ ('# Box '). load ('test. php ', {url: 'www .111cn.net'}, function (response, status, xhr) {// submit the php file to test in post mode
Console. log (response); // returned data
Console. log (status); // returned statuses success and error
Console. log (xhr );
// The returned xhr objects include:
// Xhr. responseText => is equivalent to response,
// Xhr. responseXML => returned xml data,
// Xhr. status => returned status codes 200, 400, 401, 404, and 500
// Xhr. statusText => explanation of the status code
});
}); |
I found some information on the Internet that the load function will generate a cache, so that when the file you load is changed, it will not be instantly displayed.
However, there are three methods:
1. For html files
| The code is as follows: |
Copy code |
<META HTTP-EQUIV = "Pragma" CONTENT = "no-cache">
<META HTTP-EQUIV = "Cache-Control" CONTENT = "no-cache">
<META HTTP-EQUIV = "Expires" CONTENT = "0"> |
If it is a PHP file, add
| The code is as follows: |
Copy code |
|
<? Php header ("Cache-Control: no-cache, must-revalidate");?>
$. AjaxSetup ({
Cache: false // disable the corresponding AJAX cache
}); |
3. Change the file name to another file name, so that you can read it again!
| The code is as follows: |
Copy code |
|
$ ('# Ajax'). click (function (){
Certificate ('{box'{.load('ddd.html? '+ Math. random ());
} |
In this way, we use the Math. random () function to bring parameters to the page, so there will be no cache problem.