If there is no
response entity
body
and the
asynchronous flag
is
false or the state is
LOADING
Switch to the DONE state
.
When something is said to be a
network error
run the
request error
steps for exception
NETWORK_ERR
.
When something is said to be an
abort error
run the
request error
steps for exception
ABORT_ERR
.
When something is said to be a
request error
for exception
exception
run these steps:
Set the
response entity body
to
null.
Set the the
error flag
to true.
Empty the list of
author request
headers
.
Switch the state to
DONE
.
If the
asynchronous flag
is false
raise an
exception
exception and terminate the overall set of
steps.
If the
asynchronous flag
is true
queue a task
to
dispatch a
readystatechange
event
.
Terminate the overall set of steps.
It is likely that a future version of this specification will
dispatch an
error
/
abort
event here as well. (Depending on the type of error.)
When it is said to
run these steps:
Switch the state to
HEADERS_RECEIVED
.
Queue a task
to
dispatch a
readystatechange
event
.
If the state is
UNSENT
,
OPENED
with the
send()
flag
being false, or
DONE
go to the next step.
Otherwise run these substeps:
Switch the state to
DONE
.
Set the
send()
flag
to false.
Dispatch a
readystatechange
event
.
It is likely that a future version of this specification
will dispatch an
abort
event here.
Switch the state to
UNSENT
.
No
readystatechange
event is dispatched.
4.7
Response
4.7.1
The
status
attribute
The
status
attribute
must
return the result of running these steps:
If the state is
UNSENT
or
OPENED
return 0 and terminate these steps.
If the
error flag
is true return 0 and
terminate these steps.
Return the HTTP status code.
4.7.2
The
statusText
attribute
The
statusText
attribute
must
return the result of running these steps:
If the state is
UNSENT
or
OPENED
return the empty string and terminate these steps.
If the
error flag
is true return the empty
string and terminate these steps.
Return the HTTP status text.
When the
is invoked, the user agent
must
run these steps:
If the state is
UNSENT
or
OPENED
return null and terminate these steps.
If the
error flag
is true return null and
terminate these steps.
If
header
is an
ASCII
case-insensitive
match for
Set-Cookie
or
Set-Cookie2
return null and terminate these steps.
If
header
is an
ASCII
case-insensitive
match for multiple HTTP response headers, return
the values of these headers as a single concatenated string separated
from each other by a U+002C COMMA U+0020 SPACE character pair and
terminate these steps.
If
header
is an
ASCII
case-insensitive
match for a single HTTP response header, return the
value of that header and terminate these steps.
Return null.
// The following script:
var client = new XMLHttpRequest();
client.open("GET", "test.txt", true);
client.send();
client.onreadystatechange = function() {
if(this.readyState == 2) {
print(client.getResponseHeader("Content-Type"));
// ...should output something similar to the following text:
Content-Type: text/plain; charset=utf-8
When the
method
is invoked, the user agent
must
run the following steps:
If the state is
UNSENT
or
OPENED
return the empty string and terminate these steps.
If the
error flag
is true return the empty
string and terminate these steps.
Return all the HTTP headers, excluding headers that are an
ASCII case-insensitive
match for
Set-Cookie
or
Set-Cookie2
, as a single string,
with each header line separated by a U+000D CR U+000A LF pair excluding
the status line, and with each header name and header value separated by
a U+003A COLON U+0020 SPACE pair.
// The following script:
var client = new XMLHttpRequest();
client.open("GET", "test.txt", true);
client.send();
client.onreadystatechange = function() {
if(this.readyState == 2) {
print(this.getAllResponseHeaders());
// ...should output something similar to the following text:
Date: Sun, 24 Oct 2004 04:58:38 GMT
Server: Apache/1.3.31 (Unix)
Keep-Alive: timeout=15, max=99
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/plain; charset=utf-8
4.7.5
Response Entity
The
response entity body
is the
fragment of the
entity body
received so far (
LOADING
state) or the
complete
entity body
(
DONE
state). If there is no
entity body
the
response entity body
is null.
The
text response entity body
is
a
DOMString
representing the
response entity body
. The
text response entity body
is the
return value of the following algorithm:
If the
response entity body
is
null return the empty string and terminate these steps.
Let
charset
be null.
If there is no
Content-Type
header or there is a
Content-Type
header which contains a MIME type that is
text/xml
,
application/xml
or ends in
+xml
(ignoring any parameters) use the rules set forth
in the XML specifications to determine the character encoding. Let
charset
be the determined character encoding. [
XML
]
If the
Content-Type
header contains a
text/html
MIME type follow the rules set forth in the
HTML 5 specification to determine the character encoding. Let
charset
be the determined character encoding. [
HTML5
]
If the MIME type specified by the
Content-Type
header
contains a
charset
parameter and
charset
is null
let
charset
be the value of that parameter.
The algorithms described by the XML and HTML specifications
already take
Content-Type
into account.
If
charset
is null then, for each of the rows in the
following table, starting with the first one and going down, if the
first bytes of
bytes
match the bytes given in the first
column, then let
charset
be the encoding given in the cell in
the second column of that row. If there is no match
charset
remains null.
Return the result of decoding the response entity body using
charset
. Replace bytes or sequences of bytes that are not
valid according to the
charset
with a single U+FFFD
REPLACEMENT CHARACTER character.
Authors are strongly encouraged to encode their resources
using UTF-8.
The
XML response entity body
is
either a
Document
representing the
response entity body
or null. The
XML response entity body
is the
return value of the following algorithm:
If the
response entity body
is
null terminate these steps and return null.
If a
Content-Type
is present and it does not contain a
MIME type (ignoring any parameters) that is
text/xml
,
application/xml
or ends in
+xml
terminate these steps and return null. (Do not terminate these steps if
there is no
Content-Type
header at all.)
Parse the
response entity body
into a document tree following the rules from the XML specifications.
Let the result be
parsed document
. If this fails (unsupported
character encoding, namespace well-formedness error, et cetera)
terminate these steps return null. [
XML
]
Scripts in the resulting document tree will not be
executed, resources referenced will not be loaded and no associated XSLT
will be applied.
Return an object implementing the
Document
interface
representing the
parsed document
.
4.7.6
The
responseText
attribute
The
responseText
attribute
must
return the result of running these steps:
If the state is not
LOADING
or
DONE
return the empty string and terminate these steps.
Return the
text response entity
body
.
4.7.7
The
responseXML
attribute
The
responseXML
attribute
must
return the result of running these steps:
If the state is not
DONE
return null and terminate these steps.
Return the
XML response entity
body
.
5
Exceptions
Several algorithms in this specification may result in an exception
being thrown. These exceptions are all part of the group
ExceptionCode
and use the
DOMException
object,
which is defined in DOM Level 3 Core. In addition this specification
extends the
ExceptionCode
group with several new constants as
indicated below. [
DOM3Core
]
Thus, exceptions used by this specification and not defined
in this section are defined by DOM Level 3 Core.
const unsigned short SECURITY_ERR = 18;
const unsigned short NETWORK_ERR = 19;
const unsigned short ABORT_ERR = 20;
The
SECURITY_ERR
exception is
raised if an attempt is made to perform an operation or access some data
in a way that would be a security risk or a violation of the user agent's
security policy.
The
NETWORK_ERR
exception is
raised when a network error occurs in synchronous requests.
The
ABORT_ERR
exception is raised
when the user aborts a request in synchronous requests.
These exceptions will be folded into an update of DOM Level 3
Core in due course, as they are appropriate for other API specifications
as well.
Not in this Specification
This section is non-normative.
This specification does not include the following features which are
being considered for a future version of this specification:
load
event and
onload
attribute;
error
event and
onerror
attribute;
progress
event and
onprogress
attribute;
abort
event and
onabort
attribute;
Timers have been suggested, perhaps an
ontimeout
attribute;
Property to disable following redirects;
responseXML
for
text/html
documents;
Cross-origin
XMLHttpRequest
;
responseBody
to deal with byte streams;
overrideMimeType
to fix up MIME types;
getRequestHeader()
and
removeRequestHeader()
.
References
Unless marked "Non-normative" these references are normative.
[
COOKIES
]
HTTP State Management
Mechanism
, D. Kristol, L. Montulli, editors. IETF, February
1997.
HTTP State Management
Mechanism
, D. Kristol, L. Montulli, editors. IETF, October
2000.
[
DOM2Events
]
Document
Object Model (DOM) Level 2 Events Specification
, T. Pixley,
editor. W3C, November 2000.
[
DOM3Core
]
Document Object
Model (DOM) Level 3 Core Specification
, A. Le Hors, P. Le
Hégaret, L. Wood, G. Nicol, J. Robie, M. Champion, S. Byrne,
editors. W3C, April 2004.
[
ECMAScript
]
ECMAScript
Language Specification
, Third Edition. ECMA, December 1999.
[
HTML5
]
HTML 5
(work in
progress), I. Hickson, D. Hyatt, editors. W3C, 2008.
HTML 5
(work in progress), I. Hickson, editor. WHATWG, 2008.
[
HTTPVERBSEC
]
(Non-normative)
Multiple vendors' web
servers enable HTTP TRACE method by default
, US-CERT.
(Non-normative)
Microsoft Internet
Information Server (IIS) vulnerable to cross-site scripting via HTTP
TRACK method
, US-CERT.
(Non-normative)
HTTP proxy default
configurations allow arbitrary TCP connections
, US-CERT.
[
RFC2119
]
Key words for use in RFCs
to Indicate Requirement Levels
, S. Bradner. IETF, March 1997.
[
RFC2616
]
Hypertext Transfer
Protocol -- HTTP/1.1
, R. Fielding, J. Gettys, J. Mogul, H.
Frystyk, L. Masinter, P. Leach, T. Berners-Lee, editors. IETF, June 1999.
[
RFC2617
]
HTTP Authentication: Basic
and Digest Access Authentication
, P. Hallam-Baker, J.
Hostetler, S. Lawrence, P. Leach, A. Luotonen, L. Stewart, editors. IETF,
June 1999.
[
RFC3986
]
Uniform Resource
Identifier (URI): Generic Syntax
, T. Berners-Lee, R. Fielding,
L. Masinter, editors. IETF, January 2005.
[
WebIDL
]
IDL (work in progress), C. McCormack, editor. W3C, 2009.
[
XML
]
Extensible Markup Language
(XML) 1.0 (Fifth Edition)
, T. Bray, J. Paoli, C.
Sperberg-McQueen, E. Maler, F. Yergeau, editors. W3C, November 2008.
Namespaces in XML
(Second Edition)
, T. Bray, D. Hollander, A. Layman, R. Tobin,
editors. W3C, August 2006.
Acknowledgments
The editor would like to thank Addison Phillips, Ahmed Kamel, Alex
Hopmann, Alex Vincent, Alexey Proskuryakov, Asbjørn Ulsberg, Boris
Zbarsky, Björn Höhrmann, Cameron McCormack, Christophe Jolif,
Charles McCathieNevile, Dan Winship, David Håsäther, Dean
Jackson, Denis Sureau, Doug Schepers, Douglas Livingstone, Elliotte
Harold, Eric Lawrence, Erik Dahlström, Sam Sneddon, Gideon Cohn,
Gorm Haug Eriksen, Hallvord R. M. Steen, Håkon Wium Lie, Ian Davis,
Ian Hickson, Ivan Herman, Jeff Walden, Jens Lindström, Jim Deegan,
Jim Ley, Joe Farro, Jonas Sicking, Julian Reschke, Karl Dubost, Lachlan
Hunt, Maciej Stachowiak, Magnus Kristiansen, Marc Hadley, Marcos Caceres,
Mark Baker, Mark Birbeck, Mark Nottingham, Mohamed Zergaoui, Pawel
Glowacki, Peter Michaux, Robin Berjon, Ruud Steltenpool, Simon Pieters,
Stewart Brodie, Sunava Dutta, Thomas Roessler, Tom Magliery, and Zhenbin
Xu for their contributions to this specification.
Special thanks to the Microsoft employees who first implemented the
XMLHttpRequest
interface, which was first widely
deployed by the Windows Internet Explorer browser.
Special thanks also to the WHATWG for drafting an initial version of
this specification in their Web Applications 1.0 document (now renamed to