Details about ajax. load () method in jQuery, jqueryajax. load

Source: Internet
Author: User

Details about ajax. load () method in jQuery, jqueryajax. load

JQuery load () method

The jQuery load () method is a simple but powerful AJAX method.

Load () method loads data from the server and puts the returned data into the selected element.

Syntax:

$(selector).load(URL,data,callback);

The load () function loads data from the server and replaces the content of the currently matched element with the returned html content.

The load () function uses the GET method by default. If data in the object form is provided, the function is automatically converted to the POST method.

Because the Get request method is used by default, we can also add data to the url for submission.

For example$("#box").load("loadTest.html?name=zhang&age=25")

The load () method can have three parameters:

Url (required. The url of the html file. The parameter type is String)

Data (optional) key/value data sent. The parameter type is Object)

Callback (optional, a success or failure callback Function. The parameter type is Function)

The load () method is a local method, because it requires a jQuery object containing elements as the prefix. For example, $ ("# box"). load ()

$. Get () and $. post () are global methods without specifying an element. For usage,. load () is suitable for asynchronous acquisition of static files,

$. Get () and $. post () are more suitable for passing parameters to the server page.

The optional callback parameter specifies the callback function to be allowed after the load () method is complete. The callback function can set different parameters:

  • ResponseTxt-contains the result when the call is successful
  • StatusTXT-including the call status
  • Xhr-contains XMLHttpRequest object

The following example shows a prompt box after the load () method is complete. If the load () method is successful, "external content loaded successfully" is displayed !", If it fails, an error message is displayed:

$ ("Button "). click (function () {$ ("# div1 "). load ("demo_test.txt", function (responseTxt, statusTxt, xhr) {if (statusTxt = "success") alert ("external content loaded successfully! "); If (statusTxt =" error ") alert (" Error: "+ xhr. status +": "+ xhr. statusText );});});

The above is a detailed description of ajax in jQuery. the load () method is helpful to you. If you have any questions, please leave a message and the editor will reply to you in time. Thank you very much for your support for the help House website!

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.