1. What happens when the user enters the URL and displays the page

The DNS

A TCP connection

Sending an HTTP request

The server processes the request and returns HTTP packets

The browser parses the rendered page

Connect the end of the

2. Disadvantages of JSONP

JSON only supports GET, because script tags can only use GET requests; JSONP requires backend coordination to return data in the specified format.

3. Cross domain (JSONP, Ajax)

Json: Ajax requests are affected by the same origin policy, which does not allow cross-domain requests. However, links in the SRC attribute of script tag can access cross-domain JS scripts. With this feature, the server no longer returns data in JSON format, but returns a piece of JS code that calls a function, which is called in SRC, thus realizing cross-domain

4. State returned by Ajax

0 – (uninitialized) Send () method has not been called 1 – (loaded) Send () method has been called and the request is being sent

2 – (load completed) The send() method is complete and all responses have been received

3 – (Interaction) Parsing the response content

4 – (Done) The response content is parsed and can be invoked on the client side

5. Implement an Ajax

AJAX creates the asynchronous object XMLHttpRequest

Manipulate the XMLHttpRequest object

(1) Set request parameters (request mode, relative path of request page, whether asynchronous)

(2) Set the callback function, a function that handles the server response, using onreadyStatechange, similar to the function pointer

(3) Get the readyState property of the asynchronous object: this property holds the status information of the server response. The onReadyStatechange function is executed whenever readyState changes.

(4) Judge the status of the response message. If the value is 200, the server runs normally and returns the response data.

The responseText property is used to retrieve the data returned by the server.

var xhr = new XMLHttpRequest(); xhr.open('get', 'aabb.php', true); xhr.send(null); xhr.onreadystatechange = function() { if(xhr.readyState==4) { if(xhr.status==200) { console.log(xhr.responseText); }}}Copy the code