$(function() { //alert (2); Give more hints and develop a habit test varmao=[{"Zhi1": "Mao1", "Zhi2": "Mao2"},{"Zhi3": "Mao3", "Zhi3": "Mao3"}];//This is the object array.//Put the value to pass$.ajax ({"url": "Maojiale",//Path"Type": "Post",//Way Post/get"Data": mao[1],//give subscript, otherwise the servlet page cannot be based on the key, value"DataType": "JSON",//define the data format of the reverse back"Success": HD,//callback function"Error":function() {alert ("AJAX Error!!!" "); } }); //callback function, KK, an array of objects coming from the Servlet page, or objects, values, etc., note: This can only have one parameter functionHD (KK) {//Each loop output$ (KK). each (function(i) {$ ("#mao"). Append ("id" + This. id+ "Name is" + This. name+ "Gender" + This. Sex); }); }});
Below is
The servlet page code is then combined to implement the value of the Ajax pass value
protected voidDoget (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException {doPost (request, response); } protected voidDoPost (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException {//Set character encodingRequest.setcharacterencoding ("Utf-8"); Response.setcharacterencoding ("Utf-8"); //Output ObjectPrintWriter out=Response.getwriter (); //fetch the value of the JSP pageString qu=request.getparameter ("Zhi3"); System.out.println ("Value is" +qu); List<shiti> list=NewArraylist<shiti> ();//cannot generally return objects to go, so to convert the format to, JSON formatList.add (NewShiti (1, "Zhang San", "male")); List.add (NewShiti (2, "John Doe", "male")); List.add (NewShiti (3, "I Dare", "female")); String mm=json.tojsonstring (list);//fastjsonn converting formats with jar packet serialization//Output Object Call method pass value back to JSP pageOut.print (mm); }
Because the callback function can only put one, so Out.print () also put an object, so, in the callback function, the ' original ' parameter arbitrarily defined "KK"
JS and jquery Learn--03.ajax small detail notes