Jquery Ajax load () method

Source: Internet
Author: User
Tags blank page

The load () method is the simplest and most commonly used Ajax method in jquery. It can load remote htmlCodeAnd insert it to the Dom. Its syntax structure is:

Load (URL [, data] [, callback])

The parameters of the load () method are described in the following table:

Parameter Name Type Description
URL String Request the URL of the HTML page
Data (optional) Object Key/value data sent to the server
Callback (optional) Function The callback function when the request is complete, whether the request is successful or fails.

1. load HTML documents
First, create an HTML file named test.html to prepare for background Ajax loading. The Code is as follows:

Copy code The Code is as follows: <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en" "http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<Title> insert title here </title>
</Head>
<Body>

<Div class = "comment">
Existing comments:
</Div>
<Div class = "comment">
<H6> Michael Zhang: </H6>
<P class = "para"> sofa. </P>
</Div>
<Div class = "comment">
<H6> Li Si: </H6>
<P class = "para"> bench. </P>
</Div>
<Div class = "comment">
<H6> Wang Wu: </H6>
<P class = "para"> floor. </P>
</Div>
</Body>
</Html>

Create a blank page and add two elements to it: the <button> button is used to trigger the Ajax event, and the element with ID "restext" is used to display the appended HTML content. The next step is to write jquery code. After the dommetadata component is complete, use the "laod(metadata)" button for "pull sendresponse" to load the content of test.html to the element whose ID is "restext. The Code is as follows:Copy codeThe Code is as follows: <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en" "http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<Title> insert title here </title>
<SCRIPT type = "text/JavaScript" src = "/jquery-1.3.2.js"> </SCRIPT>
</Head>
<Body>

<Input type = "button" id = "send" value = "ajax get"/>
<Div id = "restext"> </div>

<SCRIPT type = "text/JavaScript"> <! --
$ (Document). Ready (function (){
$ ("# Send"). Click (function (){
$ ("# Restext"). Load ("test.html ");
});
});
// --> </SCRIPT>

</Body>
</Html>

When the button is clicked, the following page appears:


Obviously, the load () method has completed the tedious work. Developers only need to use the jquery selector to specify the target location for HTML fragments, and then pass the URL of the file to be loaded as a parameter to the load () method.
2. filter the loaded HTML documents
In the previous example, the content of the test.html page is loaded into the element with the ID "restext. If you only need some elements in the test.html page, you can use the URL parameter of the load () method to achieve the goal. By specifying a selection character for the URL parameter, you can easily filter the required content from the loaded HTML document.
The syntax structure of the URL parameter of the load () method is "url selector ". Note that there is a space between the URL and the selector.
For example, if the class is "para" only on the test.html page, you can use the following code:
$ ("# Restext"). Load ("test.html. Para ");
The running effect is as follows:


3. Transmission Mode
The load () method is automatically specified based on the parameter data. If no parameter is transmitted, the get method is used; otherwise, the POST method is automatically converted.

Copy code The Code is as follows: // if no parameter is passed, the get method is used.
$ ("# Restext"). Load ("test. php", function (){
//......
});
// If parameters are passed, the POST method is used.
$ ("# Restext"). Load ("test. php", {name: "xht555", age: "24"}, function (){
//......
});

4. Callback Parameters
The load () method provides the callback function (callback) for operations that can continue only after loading is complete. This function has three parameters, the content returned by the request, the Request status, and the XMLHTTPRequest object are displayed respectively. The jquery code is as follows:Copy codeThe Code is as follows: $ ("# restext"). Load ("test.html", function (responsetext, textstatus, XMLHttpRequest ){
// Responsetext: Content returned by the request
// Textstatus: Request status: Success, error, notmodified, and timeout
// XMLHttpRequest: XMLHTTPRequest object
});

Note: In the load () method, no matter whether the Ajax request is successful or not, the callback function is triggered after the request is complete.

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.