I. AJAX
1. Concepts: Asynchronous JavaScript and XML
a, Asynchronous and Synchronous (based on communication between client and server)
1. Clients must wait for a response from the server and cannot do anything else while waiting
2, the client does not need to wait for a response from the server, and the client can do other things while the server processes the request
b, Ajax is a technology that updates part of a web page without reloading the entire page; by exchanging a small amount of data with the server in the background, Ajax can make the web page update asynchronously
2. Implementation (native JS)
<script> //Definition method function fun{ //Send Asynchronous Request //1. Create Core Objects var xmlhttp; //General Browser xmlhttp = new XMLHttpRequest(); /* Set up a connection Parameters: 1. Request method: GET, POST get Method: Request parameters are stitched at the back of the URL, and send method is empty post Method: Request parameters are defined in send method 2,Requested URL: 3,Synchronous or asynchronous requests: true (synchronous), false (asynchronous) */ xmlhttp.open("GET","ajaxServlet?username=zhangsan",true); //Send Request xmlhttp.send(); //POST Method //xmlhttp.send("username=zhangsan"); /* Receive and process response results from the server Get it:xmlhttp.responseText */ xmlhttp.onreadystatechange=function(){ //4 For the request to be completed and the response ready if(xmlhttp.readyState==4 && xmlhttp.status==200){ //Get response results from the server var responseText = xmlhttp.responseText; alert(responseText); } } } </script>
3. JQuery implementation
//1,$.ajax() <script> $.ajax({ url:"ajaxServlet", //Request Path type:"POST", //Request Method data:{"username=zhangsan","age=18"}, success:function(data){ alert(data); }, //Callback function after successful response error:function(){ alert("Error") } //Indicates that a callback function will be executed if an error occurs in the request loudness }); </script> //$.get():get request <script> $.get("ajaxServlet",{username:"zhangsan"},function(data){ alert(data); },"text"); </script> //$.post():post request <script> $.post("ajaxServlet",{username:"zhangsan"},function(data){ alert(data); },"text"); </script>
2. JSON
1. Concepts: JavaScript Object Representation
a. The syntax that json now uses to store and exchange text information
b. Data transfer
c, json is smaller, faster, and easier to parse than XML
2. Grammar
a. Basic Rules
1. Data in name/value pairs: json data is made up of key-value pairs of types
1-Number
2-String (in double quotes)
3-Logical value (true or false)
4-Array (in square brackets) {"person": [{}, {}]}
5-Object (in curly brackets) {"address": {}, {}
6-null
b. Data is separated by commas: multiple key-value pairs are separated by commas
c. Curly brackets to save objects: use {} to define json format
d. Square brackets save the array: []
3. Getting data
a, json objects. Key names
b, json object ["key name"]
c, Array object [index]
/* Value Setting, Getting */ var ps = [{"name":"Zhang San", "age":23,"gender":true}, {"name":"Li Si", "age":18,"gender":true}, {"name":"King Five", "age":11,"gender":true} ] for(var i = 0; i < ps.length; i++){ var p = ps[i]; for(var key in p){ alert(key+":"+p[key]); } }
4. json parser
a. Steps for use
1,Import jackson Relevant jar package 2,Establish jackson Core Objects ObjectMapper 3,call ObjectMapper Related methods for conversion //Conversion method: writeValue(parameter1,obj); //parameter1: File: take obj Object conversion to JSON String, and saves it to the specified file Writer: take obj Object conversion to JSON String, and json Data is populated in the output stream OutPutStream: take obj Object conversion JSON String, and json Data is populated in byte output stream writeValueAsString(obj): Convert Object to json Character string /* eg */ @Test public void test() throws Exception{ Person p = new Person(); p.setName("Zhang San"); p.setAge(18); p.setGender("female"); //Transformation ObjectMapper mapper = new ObjectMapper(); String json = mapper.writeValueAsString(p); System.out.println(json); }
b. Notes
1, @JsonIgonre: Exclude attributes
2, @JsonFormat: Attribute value formatting
@JsonFormat(pattern = "yyyy-MM-dd")
c. Complex java object conversion
1, List: Array
2, Map: Object formats are consistent