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!