Ajax load () method
1
Load ()The function is used to load data from the server and replace 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.
Load data from another page to the current page
<Script src = "jquery-1.11.2.js"> </script>
How to load data from the server to the current page request Page code
<Script src = "jquery-1.11.2.js"> </script> Enter the user ID:
Server code
Using System; using System. collections. generic; using System. configuration; using System. data; using System. data. sqlClient; using System. linq; using System. web; namespace jqueryTest {////// LoadHandler summary ///Public class loadHandler: IHttpHandler {public void ProcessRequest (HttpContext context) {context. response. contentType = "text/html"; string id = context. request ["id"]; string userInfo = GetByIdData (Convert. toInt32 (id); if (userInfo = "") {context. response. write ("the data you are looking for does not exist"); return;} else {context. response. write (userInfo) ;}} public string GetByIdData (int id) {string connStr = ConfigurationManager. connectionStrings ["ConnStr"]. connectionString; using (SqlConnection conn = new SqlConnection (connStr) {conn. open (); using (SqlCommand cmd = conn. createCommand () {cmd. commandText = "select * from T_UserInfo where id = @ id"; SqlParameter sp = new SqlParameter ("@ id", id); cmd. parameters. add (sp); DataTable dt = new DataTable (); using (SqlDataAdapter adapter = new SqlDataAdapter (cmd) {adapter. fill (dt);} if (dt. rows. count <= 0 | dt. columns. count <= 0) {return "";} string str = null; for (int I = 0; I <dt. columns. count; I ++) {str + = dt. rows [0] [I]. toString () + "|"; // obtain the data in column I of the first row} return str; // return the data of the first row. }}public bool IsReusable {get {return false ;}}}}
Data processed using the callback function: for example