In a common request/response model, for example, Web 1.0, a client (code running on a browser or local machine) sends a request to the server. The request is synchronous. In other words, the client waits for the response from the server. When the client waits, at least some form of notification will be sent to you:
- Hourglass (especially on Windows ).
- Rotate the ball (usually on a Mac machine ).
- The application is basically frozen, and the cursor changes over a period of time.
This is why Web applications are clumsy or slow-lack real interactivity. When you press the button, the application becomes unusable until the newly triggered request gets a response. If a request needs to be processed by a large number of servers, the waiting time may be long (at least in the world where the multi-processor and DSL are not waiting ).
Asynchronous requestsNoWait for the server to respond. The application continues to run after the request is sent. You can still enter data in a Web form or even exit the form. Applications do not obviously freeze without the rotating ball or hourglass. The server quietly responds to the request and tells the original requester that the work has been completed (the specific method will soon be seen ). The result is that the application feelsNoIt is so slow or slow, but fast response, strong interaction, and much faster. This is only part of Web 2.0, but it is a very important part. All the old GUI components and Web design specifications cannot overcome the slow and synchronous request/response model.
What is the biggest secret between Ajax and Web 2.0? The secret lies inXMLHttpRequest
A simple attributeonreadystatechange
.
Because it is an asynchronous request, the JavaScript method will not wait for the server. Therefore, the code will continue to be executed, that is, the control will be returned to the form by exiting the method. The user can continue to enter information, and the application will not wait for the server.
This raises an interesting question: what will happen after the server completes the request? If nothing happens, the server is successfullyXMLHttpRequest
After the request sent to it is processed, some instructions are required to explain how to do it.
Nowonreadystatechange
This attribute is now available. This attribute allows you to specifyCallback Function. Callback allows serversReverse callCode in the Web page. It also gives the server a certain degree of control. After the server completes the request, it will viewXMLHttpRequest
Object, especiallyonreadystatechange
Attribute. Then, call any method specified by this property. Callback is called because the server initiates a call to the webpage, regardless of what the webpage is doing. For example, this method may be called when a user is sitting in a chair without touching the keyboard, but it may also be called when the user inputs, moves the mouse, scrolls the screen, or clicks a button. It does not care about what the user is doing.
This is called asynchronous: the user operates the form on one layer, while the server responds to the request and triggersonreadystatechange
The callback method specified by the property.