AJAX and JSON (java review day19)

Keywords: JSON Javascript xml JQuery


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)

		//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)

			//Send Request

			//POST Method

				Receive and process response results from the server
				Get it:xmlhttp.responseText
				//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;

3. JQuery implementation

			url:"ajaxServlet", //Request Path
			type:"POST",  //Request Method
			}, //Callback function after successful response
			}	//Indicates that a callback function will be executed if an error occurs in the request loudness

	//$.get():get request

	//$.post():post request


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

2-String (in double quotes)
3-Logical value (true or false)
4-Array (in square brackets) {"person": [{}, {}]}
5-Object (in curly brackets) {"address": {}, {}

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){

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:
			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
	public void test() throws Exception{
		Person p = new Person();
		p.setName("Zhang San");

		ObjectMapper mapper = new ObjectMapper();
		String json = mapper.writeValueAsString(p);


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

Posted by rscott7706 on Fri, 12 Jun 2020 18:06:27 -0700