①: There's an original eco-JavaScript in front of Ajax. Here's a much simpler way to implement Ajax
Common parameters of the ②$.get () method
Parameters |
Type |
Description |
Url |
String |
Mandatory, specify the sending address |
Data |
Plainobject or string |
Optionally, specify the data that is sent by the request |
Success |
Function (Plainobject data, String textstatus,jqxhr jqxhr) |
Optional, function called after success Parameter data: Optional server returns results Parameter textstatus: Optional description request Status Parameter jqxhr: Optional is superset of XMLHttpRequest (If you specify datatype this must be selected) |
DataType |
String |
Optional: Expected data type returned by the server |
Common parameters of the ③$.post () method Ibid.
I. Word parts (JSON common words)
1.load load 2.serialize serialized 3.contains contains 4.feature feature
5.quote reference 6.skip Jump 7.transient short 8.pretty quite
9.prototype Prototype 10.conflict conflict
Two. Some common questions about JSON
1.jQuery the main way to implement Ajax
① Original Ecological Realization
②$.get () and $.post () methods
③$.getjson () method
④.load ()
2.jQuery parsing form data
The. Serializearray () method of jquery detects valid controls from a set of form elements:
① element cannot be disabled
The ② element must have a Name property
③ The selected checkbox is valid.
④ the selected radio is valid.
⑤ only the Submit button that triggered the commit event is valid
⑥file elements are not serialized
3.jQuery with other 3
Three. Implementing Ajax
1. Using the $.get () method to implement an asynchronous authentication registration mailbox
1 $ (function () {2$("#email"). blur (function () {3 varemail=$ ("#email"). Val ();4 if(email==NULL|| email==""){5$("#samp"). HTML ("The mailbox cannot be empty! ");6 }7 Else{8$.Get("Userservlet","email="+email,callback);9 function CallBack (data) {Ten if(data=="true"){ One$("#samp"). HTML ("email has been registered! "); A } - Else{ -$("#samp"). HTML ("email can be registered! "); the } - } - } - }); + -});
2. Load the Administrator page topic list using the $.getjson () method
1$.getjson ("Userservlet", "Por=top", calltopics);2 3 functioncalltopics (top) {4 5 var$userul =$ ("#userul"). empty ();6 for(vari=0;i<top.length;) {7 //alert ("ddd");8 $userul. Append (9"<li>" +top[i].topics+ " <a href=" > Modify </a> <a href= " > Delete </a></li> "Ten ); Onei++; A if(i==top.length) { - - Break; the } - } -}
3. Directly return HTML content in Ajax to generate the topic Management page
1 $.ajax ({ 2 "url": "Userservlet" , 3 " type ":" GET " 4 " data ":" Por=top1 " 5 " DataType ":" HTML " 6 "Success" : Calltopics 7 }); 8 function Calltopics (data) { 9 $ ("#userul" 10 }
4. Load the server-generated list of topics for the Administrator page using the. Load () method
$ ("#userul"). Load ("Userservlet", "POR=TOP1");
5. Use Ajax to implement non-refreshed news comment function
1 if(Por.equals ("addcom")){2 //on Machine 5 Add comment3Comment com=Newcomment ();4Commentdao comdao=NewCommentimpl ();5String name=request.getparameter ("Cauthor1");6String Ip=request.getparameter ("CIP");7String content=request.getparameter ("Ccontent");8String ctime= "2017-7-4";9 //time.tostring ();Ten com.setcname (name); One com.setccontent (content); A Com.setcip (IP); - Com.setctime (ctime); - intRe=comdao.addcomment (COM); theString result= ""; - if(re>0){ -result= "Success"; -}Else { +Result= "Add failed! "; - } + A atResponse.setcontenttype ("Text/html;charset=utf-8"); -PrintWriter out=Response.getwriter (); -Out.print ("[{\" result\ ": \" "+result+" \ ", \" ctime\ ": \" "+ctime+" \ "}]"); - Out.flush (); - out.close (); - in}
6. Load a list of topics using the Fastjson Retrofit Administrator page
Topdao nd=New Topimpl (); List<top> listtop=nd.alltop (); String Titlejson=json.tojsonstringwithdateformat (listtop, "Yyyy-mm--dd HH:mm:ss");
Four. Deepen understanding
Server-side generated JSON string code can be simplified via the Fastjson's related API
The $.parsejson () method is used to parse a JSON-formatted string into a JSON object
Welcome to the wrong, welcome to ask questions, original reprint please specify the blog park
accp8.0 Conversion Textbook 11th Chapter Jajax extension Comprehension and practice