AJAX XMLHttpRequest Object Detailed

Source: Internet
Author: User
Tags http request

The XMLHttpRequest object is the technical foundation of all today's Ajax and Web 2.0 applications. Although software distributors and open source communities are now providing a variety of AJAX frameworks to further simplify the use of XMLHttpRequest objects, we still need to understand the detailed work mechanism of this object.

First, the introduction

Asynchronous JavaScript and XML (AJAX) are a special term used to implement data interaction between client script and server. The advantage of this technique is that it provides developers with a way to retrieve data from a Web server without giving back to the server the page that the user is currently looking at. With modern browsers ' programming code (JAVASCRIPT), which accesses the DOM structure of browsers, dynamically changing the support of the displayed content, Ajax lets developers update the displayed HTML content on the browser side without having to refresh the page. In other words, Ajax makes browser-based applications more interactive and similar to traditional desktop applications.

Google's Gmail and Outlook Express are some of the two familiar examples of using AJAX technology. Also, Ajax can be used in any client-side scripting language, which includes Javascript,jscript and VBScript.

Ajax uses an object-xmlhttprequest-built into all modern browsers to deliver and receive HTTP request and response information. An HTTP request sent via the XMLHttpRequest object does not require a <form> element to be owned or mailed back in the page. "A" in Ajax represents "asynchronous," which means that the send () method of the XMLHttpRequest object can be returned immediately, allowing other html/javascript on the Web page to continue their browser-side processing while the server processes HTTP requests and sends a response. Although the request is made asynchronously by default, you can choose to send a sync request, which pauses the processing of other Web pages until the page receives a response from the server.

Microsoft introduced the XMLHttpRequest object as an ActiveX object in its Internet Explorer (IE) 5. Other browser manufacturers that recognize the importance of this object are also implementing XMLHttpRequest objects in their browsers, but as a local JavaScript object rather than as an ActiveX object. Now, having realized the value and security features of this type, Microsoft has implemented XMLHttpRequest as a Window object attribute in its IE 7. Fortunately, although the details of its implementation (and thus the way it affects the invocation) are different, all browser implementations have similar functionality and are essentially the same method. Currently, the organization is working on the standardization of XMLHttpRequest objects and has issued a draft on the norms of the consortium.

This article discusses the XMLHttpRequest object API in detail and interprets all of its properties and methods.

Ii. properties and events of XMLHttpRequest objects

XMLHttpRequest objects expose various properties, methods, and events to facilitate scripting and control of HTTP requests and responses. Below, we will discuss this in detail.

ReadyState Property

When a XMLHttpRequest object sends an HTTP request to the server, it goes through several states: Wait until the request is processed, before it receives a response. In this way, the script responds correctly to various state-xmlhttprequest objects exposing a readystate attribute that describes the current state of the object, as shown in table 1.

Table 1. A list of ReadyState property values for the XMLHttpRequest object.

ReadyState value Describe
0 Describes an "uninitialized" state, at which point a XMLHttpRequest object has been created, but has not yet been initialized.
1 Describes a "send" state, at which point the code has invoked the XMLHttpRequest open () method and XMLHttpRequest is ready to send a request to the server.
2 Describes a "send" state, at which point a request has been sent to the server side through the Send () method, but no response has been received.
3 Describes a "receiving" state in which HTTP response header information has been received, but the message body part has not yet fully received the end.
4 Describes a "loaded" state at which time the response has been fully received.
onReadyStateChange Events

The XMLHttpRequest object fires a ReadyStateChange event regardless of when the readystate value changes. Where the onReadyStateChange property receives a EventListener value-indicates to the method that the object will be activated regardless of when the readystate value has changed.

ResponseText Property

This ResponseText property contains the textual content of the HTTP response received by the client. When the readystate value is 0, 1, or 2 o'clock, ResponseText contains an empty string. When the readystate value is 3 (receiving), the response contains response information that the client has not yet completed. When ReadyState is 4 (loaded), the responsetext contains the complete response information.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.