Use of jQuery asynchronous request Ajax application $. load

Source: Internet
Author: User
Tags php file

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.

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.