The example in this article describes the method used by Js+ajax to process a JSON object loop that is returned by the Java background to a table. Share to everyone for your reference, specific as follows:
Note: Lo is the ID of the table; you need to create your own table headers, n rows, 9-column tables; var tab_id; function Varify (Cardinno) {Tab_id=document.getelementbyid ("Layer1"); Displaydiv (); tab_id.style.display= "None"; var
Url= "getrefinfoservlet?cardinno=" +cardinno;
var myajax=new ajax.request (url,{method: ' Post ', Parameters: ' 0 ', Onsuccess:dooncomplete, asynchronous:true}); var dooncomplete=function (xml_httprequest) {var msg=eval (xml_httprequest.responsetext); var info=msg.length; var row
;
var col; if (info>0) {for (Var i=0;i<info;i++) {row = L0.insertrow (L0.rows.length), Row.insertcell (0). InnerHTML = " " +
MSG[I].A;
Row.insertcell (1). InnerHTML = " " +MSG[I].B;
Row.insertcell (2). InnerHTML = " " +MSG[I].C;
Row.insertcell (3). InnerHTML = " " +MSG[I].D;
Row.insertcell (4). InnerHTML = " " +MSG[I].E;
Row.insertcell (5). InnerHTML = " " +MSG[I].F;
Row.insertcell (6). InnerHTML = " " +MSG[I].G;
Row.insertcell (7). InnerHTML = " " +MSG[I].H;
Row.insertcell (8). InnerHTML = " " +MSG[I].I;
Row.setattribute ("Align", "center"); } tab_id.style.display= "Block"; }else{tab_id.innerhtml= "<center> Sorry, the card is not open, to obtain rental car information!"
</center> ";
Tab_id.style.width = "80%";
Tab_id.style.position = "absolute";//Absolute position display tab_id.style.display= "block";
} removediv ();
}//Mask layer displays displaydiv=function () {var mybg = document.createelement ("div");
Mybg.setattribute ("id", "MYBG");
Mybg.innerhtml= "<center> is in process, please wait </img></center>";
Mybg.style.width = "100%";
Mybg.style.position = "absolute";//Absolute position display mybg.style.top = "260";
Mybg.style.left = "0";
Mybg.style.zIndex = "M";//z axis position mybg.style.opacity = "0.8";//Transparency Mybg.style.filter = "Alpha (opacity=80)";//filter Display Transparency
Document.body.appendChild (MYBG);
}//Mask layer Remove removediv=function () {document.body.removeChild (MYBG);}
Java background deposit to JSON objects:
/** * Get the latest rental return record * According to the card code * Returns the JSON object * @param cardinno * @return * * Public String Getrefinfo (string cardinno) {StringBuffer
Bf=new StringBuffer (1024);
Bf.append ("");//sql statement System.out.println (bf.tostring ());
/************ stores JSON objects ***************/list<jsonobject> jsonlist=new arraylist<jsonobject> ();
Try {/**************** obtains the judgment information in the array *******************/String [] [] Str=getarraybyname (Bf.tostring ());
if (Str!=null) {for (int i = 0; i < str[0].length i++) {jsonobject returnvalue =new jsonobject (); Returnvalue.put ("A", Str[0][i]). put ("B", Str[1][i]). Put ("C", Str[2][i].trim (). Equals ("")? "": Str[2][i].substring ( 0)). Put ("D", Str[3][i].trim (). Equals ("")? ": str[3][i].substring (0,). Put (" E ", Str[4][i]). Put (" F ", St
R[5][i]). Put ("G", str[6][i])-Put ("H", Str[7][i]). Put ("I", str[8][i));
Jsonlist.add (returnvalue);
}}catch (Exception e) {e.printstacktrace ();} System.out.println (Jsonlist.tostring ());
return jsonlist.tostring ();
}
Hopefully this article will help you with Ajax programming.