Ajax is "Asynchronous JavaScript and XML" (Asynchronous JavaScript and XML)
AJAX enables Web pages to be updated asynchronously by exchanging small amounts of data in the background with the server. This means that you can update a part of a webpage without reloading the entire page.
XMLHttpRequest is used to exchange data with the server in the background. The XMLHttpRequest object provides full access to the HTTP protocol, including the ability to make POST and HEAD requests, as well as normal GET requests. XMLHttpRequest can return the response of the WEB server synchronously or asynchronously, and can return content as text or as a DOM document. Although named XMLHttpRequest, it is not limited to working with XML documents: It can receive any form of text document.
All modern browsers (ie7+, Firefox, Chrome, Safari, and Opera) have built-in XMLHttpRequest objects.
The old version of IE browser (ie5,6) ActiveX object.
We need to know a protocol: HTTP (Hypertext Transfer Protocol), which is a stateless protocol that does not establish a persistent link, each time a connection is requested to the Web server, the response is disconnected, and the next request needs to reestablish the connection.
How to send an HTTP request with a XMLHttpRequest object:
Open (Method,url,async)
Method: Represents the request type, get,post (case insensitive)
URL: The location of the file on the server, either a relative address or an absolute address
Async:true (synchronous), False (asynchronous), default is Async
Send (String)
There is a parameter, for a GET request, its data is spelled in the URL address, so the method is limited to the POST method
Here are some examples:
Analysis: for the second kind, the POST request does not give the parameter, in the actual meaning is not big, usually is the third kind, send some information.
setRequestHeader Set HTTP header information, tell the Web server, to send a form, note the location, to be placed in the middle of open and send, otherwise, will throw an exception.
How to get the response from the server:
We can listen for changes in the ReadyState property, which represents a change in the server response
This event can be triggered by onreadystatechange this event to monitor the change of the property, and each time the ReadyState property changes. We generally listen to whether the response is completed, mainly listening to the ReadyState property status of 4
When the response is complete and the request is sent successfully, we can do things like get the contents of the server response and then make some rendering on the page
JSON (JavaScript object Notation) JavaScript Objects notation
JSON is the syntax for exchanging textual information, like XML, organized by key-value pairs, easy for people to read and write, and easily parsed and generated by machines.
JSON is language-independent, meaning that whatever language can be parsed into JSON, just follow the JSON rules.
The advantages of JSON: 1,json is shorter than XML, and transmission in the network can greatly reduce the bandwidth,
2,json Read and write faster
3,json can be easily parsed using JavaScript built-in methods, and converted to JavaScript objects.
Writing format for JSON: name/value pairs
The name is written in front, written in double quotes, the value pair is written in the back, and is also written in double quotation marks. The middle is separated by a colon.
The JSON values can be of the following types:
- Number (integers and floating-point numbers)
- String (in double quotes)
- Logical value (True false)
- Array (in square brackets)
- Object (in curly braces)
- Null
The above is a typical JSON string, first placed in a curly brace, is a JSON object, the object has a value pair, the name is "Staff", the value is an array, the array has a lot of small JSON objects, each object has two properties
Ajax Full Contact