XMLHttpRequest对象提供了许多属性、方法和事件,因此Ajax脚本可以处理和控制Http请求和响应。本章的其余部分会对这些进行详细的讨论。
The XMLHttpRequest object exposes various properties, methods, and
events so Ajax scripts can process and control HTTP requests and
responses. The rest of this chapter discusses these in detail.
1、 The readyState Property
The XMLHttpRequest object cycles through several states as it sends an
HTTP request to the server, waits while the request is processed, and when
it receives a response. So that scripts can respond appropriately to the
various states, the object exposes a readyState property that represents
the object's current state, as shown in Table 1.1.
Table 1.1 ReadyState Property Values
ReadyState Property Value Description
0
Represents an “uninitialized” state in which an
XMLHttpRequest object has been created, but
not initialized.
1
Represents a “sent” state in which code has
called the XMLHttpRequest open() method
and the XMLHttpRequest is ready to send a
request to the server.
2
Represents a “sent” state in which a request
has been sent to the server with the send()
method, but a response has not yet been
received.
3
Represents a “receiving” state in which the
HTTP response headers have been received,
but message body has not yet been completely
received.
4
Represents a “loaded” state in which the
response has been completely received.
2、 The onreadystatechange Property
The XMLHttpRequest object generates a readystatechange
event whenever the readyState value changes. The
onreadystatechange property accepts an EventListener value,
specifying the method that the object will invoke whenever the
readyState value changes.
3、 The responseText Property
The responseText property contains the text of the HTTP response
received by the client. When the readyState value is 0, 1, or 2
responseText contains an empty string. When the readyState
value is 3 (Receiving), the response contains the incomplete response
received by the client. When readyState is 4 (Loaded) the
responseText contains the complete response.
4、 The responseXML Property
The responseXML property represents the XML response when the
complete HTTP response has been received (when readyState is 4),
when the Content-Type header specifies the MIME (media) type as
text/xml, application/xml, or ends in +xml. If the Content-Type
header does not contain one of these media types, the responseXML
value is null. The responseXML value is also null whenever the
readyState value contains any value other than 4. The responseXML
property value is an object of type Document interface, and represents
the parsed document. If the document cannot be parsed (for example if the
document is malformed or the character encoding of the document is not
supported) the responseXML value is null.
5、 The status Property
The status property represents the HTTP status code2 and is of type
short. The status attribute is available only when the readyState
value is 3 (Receiving) or 4 (Loaded). Attempting to access the status
value when readyState is less than 3 raises an exception.
6、 The statusText Property
The statusText attribute represents the HTTP status code text and is
also available only when the readyState value is 3 or 4. Attempting to
access the statusText property for other readyState values raises
an exception.