Home >Web Front-end >Front-end Q&A >What are the statuses of ajax requests?

What are the statuses of ajax requests?

青灯夜游
青灯夜游Original
2022-01-19 17:35:563546browse

The status of the ajax request: 1. "Uninitialized" means that the send() method has not been called yet; 2. "Loading" means that the send() method has been called and the request is being sent; 3. "Loading" "Enter completed"; 4. "Interactive", indicating that the response content is being parsed; 5. "Complete", indicating that the response content parsing is completed and can be called on the client.

What are the statuses of ajax requests?

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

Native ajax request writing method:

   var http = new XMLHttpRequest();
    http.open('get','./package.json');
    http.onreadystatechange = function () {
        if(http.readyState == 4 && http.status == 200) {
           var result = JSON.parse(http.responseText);
            console.log(result.name)
        }
    };
    http.send();

5 states of ajax request (readyState)

  • 0 - (Uninitialized ) The send() method has not been called yet

  • 1 - (Loading) The send() method has been called and the request is being sent

  • 2 - (Loading completed) The send() method has been executed and all response content has been received

  • 3 - (Interaction) The response content is being parsed

  • 4 - (Complete) The response content parsing is completed and can be called on the client

readyState status description

(0)Uninitialized

This stage confirms whether the XMLHttpRequest object is created and does not call the open() method to prepare for uninitialization. A value other than 0 indicates that the object already exists, otherwise the browser will report an error --- the object does not exist.

(1) Loading

At this stage, the xml (standardization is getting closer) HttpRequest object is initialized, that is, the open() method is called, according to the parameters (method, url, true) Complete setting of object status. And call the send() method to start sending requests to the server. A value of 1 indicates that a request is being sent to the server.

(2) Loading completed

At this stage, the response data from the server is received. But what is obtained is only the original data of the server response and cannot be used directly on the client. A value of 2 indicates that all response data has been received. And prepare for the next stage of data analysis.

(3)Interaction

This stage parses the received server-side response data. That is, according to the MIME type returned by the server-side response header, the data is converted into a format that can be accessed through the responseBody, responseText or responsexml (standardization is getting closer) attributes, and is ready for invocation on the client. Status 3 indicates that the data is being parsed.

(4)Complete

This stage confirms that all data has been parsed into a format usable by the client, and the parsing has been completed. A value of 4 indicates that the data parsing is completed and the data can be obtained through the corresponding attributes of the xml (standardization is getting closer) HttpRequest object.

[Related tutorial recommendations: AJAX video tutorial]

The above is the detailed content of What are the statuses of ajax requests?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn