AJAX

Js is used to send requests and receive responses

The browser adds an XMLHttpRequest function to the window

This constructor can be used to construct an object

JS through it to achieve the request, receive the response

  • First of all,

Prepare a server

Use server.js as our server

Download or assign the code to start with Node server.js 8888 or node-dev server.js 8888 (which updates your code in real time)

AJAX

  • It’s a browser feature

  • Microsoft IE5 rolled out

Node global development automatically saves the installation

yarn global add node-dev

node-dev server.js 8888

Four steps

1.

Create an HttpRequest object (full name: XMLHttpRequest)

XMLHttpRequest

Used to interact with the server. With XMLHttpRequest, you can request a specific URL to retrieve data without refreshing the page. This allows the web page to update parts of the page without affecting the user’s actions.

2.

Calling objectopenmethods

3.

Listen for the onLoad & onError event of the object

It is generally recommended to use the onReadyStatechange event instead to manipulate the contents of the HTML file in the event handler example

4.

Call the object’s Send method (to send the request)

Ex. :

getHTML.onclick = () => { const request = new XMLHttpRequest() request.open('GET', '/3.html') request.onload = () => {// console.log(request.response) // Prints out the request const div = document.createElement("div") / / create a div tags div. InnerHTML = request. The response / / fill in div contents document. The body. The appendChild (div) / / inserted at the end of the inside} request. Onerror = () = > {} request.send() }Copy the code