Javascript + ajax processes the json object returned by the java background and cyclically creates a table. ajaxjson
The example in this article describes how js + ajax processes json objects returned by the java background and cyclically creates a table. We will share this with you for your reference. The details are as follows:
// Note: LO is the table id. You must create a table with n rows and 9 columns as the table header. 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, this The card has not been activated. You cannot obtain the rental and return information! </Center> "; tab_id.style.width =" 80% "; tab_id.style.position =" absolute "; // display tab_id.style.display =" block ";} removeDiv ();} // display displayDiv = function () {var mybg = document at the mask layer. createElement ("div"); mybg. setAttribute ("id", "mybg"); mybg. innerHTML = "<center> processing... please wait </img> </center>"; mybg. style. width = "100%"; mybg. style. position = "absolute"; // The absolute position is mybg. style. top = "260"; mybg. style. left = "0"; mybg. style. zIndex = "100"; // Z axis position mybg. style. opacity = "0.8"; // transparency mybg. style. filter = "Alpha (opacity = 80)"; // The filter shows the transparency document. body. appendChild (mybg);} // remove removeDiv = function () {document. body. removeChild (mybg );}
Json objects stored in the java Background:
/*** Get the latest record for renting and returning vehicles based on the internal code * return 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 ()); /*************************/List <JSONObject> jsonList = new ArrayList <JSONObject> (); try {/***************** obtain judgment information and save it to 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, 19 )). put ("d", str [3] [I]. trim (). equals ("")? "": Str [3] [I]. substring (0, 19 )). put ("e", str [4] [I]). put ("f", str [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 ();}
I hope this article will help you with ajax programming.