In jquery, ajax is widely used. If there are $. ajax, $. get, $. post, and so on, I will give you some usage instructions for these functions.
Jquery + ajax/"target =" _ blank "> jquery ajax is often used, but sometimes you forget how to write it. Take a note below. Jquery ajax example:
The Code is as follows: |
Copy code |
// Post method $. Ajax ({ Url: 'test. php ', Type: 'post ', Data: {'ac': 'addvideo', 'videoname': videoname}, // json object is used here Success: function (data ){ // Code here... }, Fail: function (){ // Code here... } }); // Get Method $. Get ("test. php", {name: "John", time: "2"}, function (data) {// code here ...}); // Get method 2 $. Ajax ({ Url: 'test. php ', Data: {'ac': 'addvideo', 'videoname': videoname}, // json object is used here Success: function (data ){ // Code here... } }); // Jquery ajax synchronization mode $. Ajax ({ Url: 'test. php ', Type: 'post ', Async: false, // use the synchronous method Data: {'ac': 'addvideo', 'videoname': videoname}, // json object is used here Success: function (data ){ // Code here... }, Fail: function (){ // Code here... } }); |
Instance
Example of jQuery Ajax refreshing form submission
The HTML code is as follows. In view of portability, no HTML mark is written.
The Code is as follows: |
Copy code |
[Html] <? Php Header ('content-Type: text/html; charset = UTF-8 ′); ?> <Script type = "text/javascript" src = "http://code.jquery.com/jquery.min.js"> </script> <Script type = "text/javascript"> $ (Function (){ $ ("# Subbtn"). click (function (){ Var params = $ ('input'). serialize (); Var url = "your server-side php "; $. Ajax ({ Type: "post ", Url: url, DataType: "json ", Data: params, Success: function (msg ){ Var tishi = "Your submitted name is:" + msg. name + "<Br/> the password you submitted is:" + msg. password; Certificate ("#tishi”).html (tishi ); ((“Tishi”).css ({color: "green "}); } }); }); }); </Script> <P> <label for = "name"> name: </label> <Input id = "name" name = "name" type = "text"/> </P> <P> <label for = "password"> password: </label> <Input id = "password" name = "password" type = "password"/> </P> <Span id = "tishi"> </span> <P> <input id = "subbtn" type = "button" value = "ajax test"/> </p> [/Html] The server PHP code is as follows: [Php] <? Php Echo json_encode ($ _ POST ); [/Php] |
Example of ajax data acquisition through json
The Code is as follows: |
Copy code |
<Script type = "text/javascript"> $ (Document). ready (function (){ GetScatalog ("paidang", "M06 ″);
}); Function getScatalog (selectid, BaseCode ){ If (BaseCode! = "") { $. Ajax ({ Url: "ajax/getCatalogByBasecode. aspx ", Data: "code =" + encodeURI (BaseCode), cache: false, Datatype: "html ", Success: function (context ){ Fillselect (selectid, context ); } }); } Else { Return "Error "; } } Function fillselect (selectid, context ){ Var listitem = new Array (); Listitem = eval (context ); For (var I = 0; I <listitem. length; I ++ ){ $ ("#" + Selectid ). append ("<option value = '" + listitem [I] ["code"] + "'>" + listitem [I] ["name"] + "</option> "); // append an Option to Select (drop-down ) } } </Script> Html code: <Select id = "paidang" name = "paidang"> <Option value = "" selected >== select ==</option> </Select> |
Ajax:
Create a new. aspx page and delete the html code on the. aspx page. Add the following code to. aspx. cs:
The Code is as follows: |
Copy code |
String rq_basecode = null; Rq_basecode = Request. QueryString ["code"]; If (string. IsNullOrWhiteSpace (rq_basecode )) { Response. Write ("Error "); Response. End (); } BLLCataLog bll_info = new BLLCataLog (); List <Scatalog> lt_info = new List <Scatalog> (); Lt_info = bll_info.GetCatalog (rq_basecode ,""); // Response. Write (rq_basecode ); If (lt_info.Count> 0) { Response. Write (JsonHelper. ToJson (lt_info )); } Else { Response. Write ("Null "); } Data at the BLL layer: Public List <M2Model. Scatalog> GetCatalog (string code, string refcode) { DALCataLog dalcatalog6 = new M2SharpDAL. DALCataLog (); Return dalcatalog6.GetCatalog (code, refcode ); } |