Ajax operations
Create XMLHttpRequest object
XT = var null ; // Step ajax engine objects obtained IF (window.XMLHttpRequest) { // create objects ajax The browser version XT = new new the XMLHttpRequest (); // create a new version of embodiment } the else { XT = new new ActiveXObject ( "Microsoft.XMLHTTP"); // old way to create versions }
The state is bound to trigger a function
// Step bind listening xt.onreadystatechange = Call; // here to force a callback function
Establish a connection to the server using the open method
// Step bound address xt.open ( "the GET", "/ AjaxTest / AjaxServlet name = Colin?", To true ); // first parameter refers to a request for the second argument refers to the first path refers to the three parameters is not true false synchronous asynchronous xt.open ( "the POST", "/ AjaxTest / AjaxServlet", to true ); POST request method xt.setRequestHeader ( "the Content-the Type", "file application / X-WWW-form -urlencoded " ); also need to add a request header meaning indicating encoding client submitted to the server are URL encoded text
Send data to the server
get request mode
xt.send ();
POST request method
xt.send ( "username =" + name);
Processing the data callback function returns
// create a callback function does not determine the state before the function Call () { var text = xt.responseText; Alert (text); } status determination based on // the HTTP server response status code [200_ normal; [400 angstroms e.g. request error: request syntax]; 403_ no access; 404_ access the resource does not exist; 500_ internal server error} // properties readystate response status codes the XMLHttpRequest object 5 5 [0_XMLHttpRequest has not completed initialization; 1_XMLHttpRequest object starts transmission request; 2_XMLHttpRequest object request transmission completed; 3_XMLHttpRequest object starts to read response; 4_XMLHttpRequest object read response ends] after determining the state of function Call () { IF (xt.readyState == == 200 is xt.status &&. 4 ) { var text = xt.responseText ; Alert (text); } }
The sample code
<input type = "button" value = "get request" the onclick = "Test ()"> </ Button> <Script> var XT = null ; var A = 0 ; function Test () { IF (window.XMLHttpRequest) { XT = new new the XMLHttpRequest (); } the else { XT = new new the ActiveXObject ( "Microsoft.XMHTTP" ); } //// properties readystate the response status code 5 in the XMLHttpRequest object 5 // [0_XMLHttpRequest not completed initialization; // 1_XMLHttpRequest Object start sending requests; //2_XMLHttpRequest object request to send completed; // 3_XMLHttpRequest object begins to read the response; // 4_XMLHttpRequest object read response ends] // change the state of the trigger onreadystatechange method xt.onreadystatechange = Call; xt.open ( "GET", "/ the Upload? = ZYM name ", to true ) xt.send () } function Call () { IF (xt.readyState == == 200 is xt.status. 4 & ) { // add this condition must be determined to take a value var the responseText = XT. the responseText; // the console.log (the responseText); Alert (+ the responseText) } } </script>
json
Import jar package
commons-beanutils.jar provides java reflection packet
commons-collections.jar collections extended package
commons-lang.jar Enhanced Kit
commons-logging.jar provides a package of the log implemented
ezmorph-1.0.4.jar
JSON-lib -2.2.2-jdk15.jar
Pserson the object into a string json
public static String objectToJson(Person p){ String str = null; JSONObject jo = JSONObject.fromObject(p); str = jo.toString();//转成字符串 return str; }
Converting the string to an object json
public static the Person jsonToPerson (String STR) { // first string into a string json the JSONObject Object = JSONObject.fromObject (STR); // in turn to the person object json string toBean (); the Person P = (the Person) the JSONObject .toBean (Object, the Person. class ); return P; }
The collection of arrays is converted into json
public static String listToJson(List<Person> li){ String str = null; JSONArray ja = JSONArray.fromObject(li); str = ja.toString(); return str; }
Converting the array into a set json
public static List <the Person> jsonToList (String STR) { List <the Person> List = new new the ArrayList <the Person> (); // get a new set of objects the JSONArray JA = JSONArray.fromObject (STR); // now string converted into JSONArray JSONObject Jo = null ; for ( int I = 0; I <ja.size (); I ++) { // iterate JSONArray Jo = ja.getJSONObject (I); // get every JSONArray JSONObject in the list. the Add ((the Person) JSONObject.toBean (Jo, the Person. class )); // in converting each bundle JSONObject UN as an object to the collection } return List; }
Converting the array into an array json
public static Person[] jsonToArray(String str){ JSONArray ja = JSONArray.fromObject(str); Person[] p = (Person[]) JSONArray.toArray(ja, Person.class); return p; }
Converting the map to set string json
public static String mapToJson(Map<Integer,Person> m){ return JSONArray.fromObject(m).toString(); }