The load () method loads data from the server through Ajax requests and places the returned data in the specified element..
The URL specifies the URL to which the request is sent.
Data is optional. Specifies the data sent to the server together with the request.
Function (response, status, xhr)
Additional parameters:
Response-contains the result data from the request
Status-contains the Request status ("success", "notmodified", "error", "timeout" or "parsererror ")
Xhr-contains XMLHTTPRequest object
Remember: you cannot use $ (this) to call the load method, because $ (this) at this time is not the result you want... it may be jquer. js.
For the following paths, see:
Usage 1 GET request
The sample code is as follows:
<% @ Include file = "/common/meta. JSP "%> <LINK rel =" stylesheet "href =" style/mystyle.css "type =" text/CSS "> <SCRIPT src =" scripts/jquery-1.6.2.min.js "type =" text/ javaScript "> </SCRIPT> <SCRIPT type =" text/JavaScript ">$ (function () {$ ("# btnajaxget "). click (function (event) {// send a GET request: You can specify the items in the file to be loaded. Use the jquery selector. Common options include ID, class, and metaselector, see the following call var username = encodeuri ($ ("# username "). val (); // $ ("# Di Vresult "). Load (" jqueryload? Username = "+ username +" & UN = "+ $ (" # username "). val () + "& timestamp =" + (new date ()). gettime (); // $ ("# divresult "). load ("$ {CTX}/data/sample.dom.html. mylist "); // $ (" # divresult "). load ("$ {CTX}/data/sample.dom.html IMG"); // $ ("# divresult "). load ("Data/sample.dom.html Div [Title = 'mytitle1']"); $ ("# divresult "). load ("Data/sample.dom.html # ages"); // $ ("# divresult "). load ("Data/sample.dom.txt"); // $ ("# divresult "). load ("index. JSP ") ;}) </SCRIPT>
Jqueryloadservlet. Java
Public class jqueryloadservlet extends httpservlet {Private Static final long serialversionuid = 1l; Public void doget (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {dopost (request, response );} public void dopost (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {request. setcharacterencoding ("UTF-8"); response. setcontenttype ("text/html; charset = UTF-8"); response. setcharacterencoding ("UTF-8"); printwriter out = response. getwriter (); string v = request. getparameter ("username"); string username = java.net. urldecoder. decode (string) request. getparameter ("username"), "UTF-8"); system. out. println ("V:" + V + ", Username:" + username); stringbuilder tables = new stringbuilder (); tables. append ("<Table id = 'AGES 'border = '0' cellspacing = '1'>"); tables. append ("<thead>"); tables. append ("<tr>"); tables. append ("<TH> export agetables </Th>"); tables. append ("<TH> typetables </Th>"); tables. append ("<TH> deletedtables </Th>"); tables. append ("</tr>"); tables. append ("</thead>"); tables. append ("<tbody>"); tables. append ("<tr>"); tables. append ("<TD> javatables </TD>"); tables. append ("<TD> statictables </TD>"); tables. append ("<TD> 1995 tables </TD>"); tables. append ("</tr>"); tables. append ("<tr>"); tables. append ("<TD> rubytables </TD>"); tables. append ("<TD> dynamictables </TD>"); tables. append ("<TD> 1993 tables </TD>"); tables. append ("</tr>"); tables. append ("<tr>"); tables. append ("<TD> smalltalktables </TD>"); tables. append ("<TD> dynamictables </TD>"); tables. append ("<TD> 1972 tables </TD>"); tables. append ("</tr>"); tables. append ("<tr>"); tables. append ("<TD> C ++ tables </TD>"); tables. append ("<TD> statictables </TD>"); tables. append ("<TD> 1983 tables </TD>"); tables. append ("</tr>"); tables. append ("</tbody>"); tables. append ("</table>"); // out. println (username + "" + new Java. util. random (). nextint (100); out. print (tables. tostring (); out. flush (); out. close ();}}
Note: Use
$ ("# Divresult"). Load ("jqueryload? Username = "+ username +" & UN = "+ $ (" # username "). val () + "& timestamp =" + (new date ()). gettime ());
When sending a parameter, you must perform the second encoding operation on the parameter:
VaR username = encodeuri ($ ("# username"). Val ()));
Perform corresponding decoding operations in the background:
String v = request. getparameter ("username"); string username = java.net. urldecoder. Decode (string) request. getparameter ("username"), "UTF-8 ");
Usage 2: POST request
"). Click (function (event) {var username = $ ("# username "). val (); // send the POST request $ ("# divresult "). load ("$ {CTX}/jqueryload", {"username": username });});
The above calls will not generate garbled characters!
Usage 3 Use callback function JS Code
- $ ("# Btnajaxcallback"). Click (function (event ){
- VaR username = $ ("# username"). Val ();
- // Send a POST request, and then execute the callback function.
- $ ("# Divresult"). Load ("$ {CTX}/jqueryload", {"username": username}, function (responsetext, textstatus, XMLHttpRequest)
- {
- Responsetext = "add in the callback function! <Br/> "+ responsetext +" <br/> "+ textstatus +", "+ XMLHttpRequest. Status +" <br/> "+ XMLHttpRequest. statustext;
- $ ("# Divresult" ).html (responsetext); // or: Response (this).html (responsetext );
- });
- });
I. How to use data
1. Load a PHP file without passing parameters.
$("#myID").load("test.php");
// Import the result after test. php is run in the element whose ID is # myid
2. Load a PHP file that contains a passing Parameter
$("#myID").load("test.php",{"name" : "Adam"});
// The imported PHP file contains a passing parameter, similar to: Test. php? Name = Adam
3. Load a PHP file that contains multiple passing parameters. Note: parameters are separated by commas (,).
$("#myID").load("test.php",{"name" : "Adam" ,"site":"61dh.com"});
// The imported PHP file contains a passing parameter, similar to: Test. php? Name = Adam & Site = 61dh.com
4. Load a PHP file. The php file uses an array as the transmission parameter.
$("#myID").load("test.php",{'myinfo[]', ["Adam", "61dh.com"]});
// The imported PHP file contains an array for passing parameters.
Note: Useload
These parameters are passed in post mode. Therefore, get cannot be used to obtain parameters in test. php.
Ii. How to Use callback
For exampleLoad Method
After receiving the server response, you can usecallback
Function. The Code is as follows:
$("#go").click(function(){
$("#myID").load("welcome.php", {"lname" : "Cai", "fname" : "Adam", function(){
$("#myID").fadeIn('slow');}
);
});
Note:
Add a space in the load URL and you will be able to follow the selector.
For example:
$("body").load("test.html #a");