When it comes to Ajax, of course, JSON format. In the process of data transmission of Ajax, the most used is the JSON format.
What is the JSON format, at the beginning of the use of it, it really makes people confused for a while. That's a little white. So-called JSON is not a form of data.
As long as we understand this format, we can use Ajax to facilitate the transfer of data. When it comes to JSON format, we also talk about objects and arrays in JS. So knowledge is all about each other.
Contact, the mastery of knowledge is still very important.
The JSON syntax is a subset of the JavaScript object representation syntax. Although JSON is simply the object and array in JS. But using Ajax to pass the JSON format to the background
The data is still going to be converted. Otherwise, the data cannot be successfully obtained in the background.
Front desk JS Code
var daytimeobj={};//declaring a JS objectvar daytime=[1,2,3,4];//declaring a JS arrayDaytimeobj.rows=daytime;//Properties$.ajax ({URL:"Insertdata.do", type:"POST", DataType:"JSON", data:{"Daytimeobj": Json.stringify (Daytimeobj),}, Success:function (data) {if(data) {alert ("OK"); } });
Get JSON data in the background
Background Java code
Import Net.sf.json.JSONArray;
Import Net.sf.json.JSONObject;
Response.setcontenttype ("Text/json; Charset=utf-8 "); String daytimeobj=request.getparameter ("Daytimeobj"); Jsonobject TT=jsonobject.fromobject (daytimeobj); // encapsulate as JSON object Object a=tt.get ("Rows"); Jsonarray jsonArray2=jsonarray.fromobject (a); // encapsulate an array of objects for (int i = 0; i < jsonarray2.size (); i++) { // loop to get the value of the array }
Ajax Examples (2)