XMLHttpRequest
W3C Working Draft 6 December 2012
-
This Version:
- http://www.w3.org/TR/2012/WD-XMLHttpRequest-20121206/ Latest Version:
- http://www.w3.org/TR/XMLHttpRequest/ Latest Editor Draft:
- http://dvcs.w3.org/hg/xhr/raw-file/tip/Overview.html Previous Versions:
- http://www.w3.org/TR/2012/WD-XMLHttpRequest-20120117/
- http://www.w3.org/TR/2011/WD-XMLHttpRequest2-20110816/
- http://www.w3.org/TR/2010/WD-XMLHttpRequest2-20100907/
- http://www.w3.org/TR/2009/WD-XMLHttpRequest2-20090820/
- http://www.w3.org/TR/2008/WD-XMLHttpRequest2-20080930/
- http://www.w3.org/TR/2008/WD-XMLHttpRequest2-20080225/
- http://www.w3.org/TR/2007/WD-XMLHttpRequest-20071026/
- http://www.w3.org/TR/2007/WD-XMLHttpRequest-20070618/
- http://www.w3.org/TR/2007/WD-XMLHttpRequest-20070227/
- http://www.w3.org/TR/2006/WD-XMLHttpRequest-20060927/
- http://www.w3.org/TR/2006/WD-XMLHttpRequest-20060619/
- http://www.w3.org/TR/2006/WD-XMLHttpRequest-20060405/ Editor:
- Julian Aubourg ( Creative Area) < j@ubourg.net>
- 송정기(Jungkee Song) ( Samsung Electronics Co., Ltd.) < jungkee.song@samsung.com>
- Hallvord R. M. Steen ( Opera Software ASA) < hallvord@opera.com> Previous Editor:
- Anne van Kesteren ( Opera Software ASA) < annevk@annevk.nl>
Abstract
The XMLHttpRequest specification defines an API that provides scripted client functionality for transferring data between a client and a server.
Status of this Document
This section describes the status of this document at the time of its publication. Other documents may supersede this document. A list of current W3C publications and the latest revision of this technical report can be found in the W3C technical reports index at http://www.w3.org/TR/.
If you wish to make comments regarding this document in a manner that is tracked by the W3C, please submit them via using our public bug database, or please send comments topublic-webapps@w3.org (archived) with [XHR] at the start of the subject line.
The bulk of the text of this specification is also available in the WHATWG XMLHttpRequest Living Standard, under a license that permits reuse of the specification text.
The W3C Web Applications Working Group is the W3C working group responsible for this specification's progress along the W3C Recommendation track. This specification is the 6 December 2012 Working Draft.
Publication as a Working Draft does not imply endorsement by the W3C Membership. This is a draft document and may be updated, replaced or obsoleted by other documents at any time. It is inappropriate to cite this document as other than work in progress.
Work on this specification is also done at the WHATWG. The W3C Web Applications working group actively pursues convergence of XMLHttpRequest specification with the WHATWG.
This document was produced by a group operating under the 5 February 2004 W3C Patent Policy. W3C maintains a public list of any patent disclosures made in connection with the deliverables of the group; that page also includes instructions for disclosing a patent. An individual who has actual knowledge of a patent which the individual believes containsEssential Claim(s) must disclose the information in accordance with section 6 of the W3C Patent Policy.
This document supersedes XMLHttpRequest 1.
Table of Contents
- 1 Introduction
- 2 Conformance
- 3 Terminology
- 4 Interface
XMLHttpRequest
- 4.1 Task sources
- 4.2 Constructors
- 4.3 Garbage collection
- 4.4 Event handlers
- 4.5 States
- 4.6 Request
- 4.7 Response
- 4.7.1 The
status
attribute - 4.7.2 The
statusText
attribute - 4.7.3 The
getResponseHeader()
method - 4.7.4 The
getAllResponseHeaders()
method - 4.7.5 Response entity body
- 4.7.6 The
overrideMimeType()
method - 4.7.7 The
responseType
attribute - 4.7.8 The
response
attribute - 4.7.9 The
responseText
attribute - 4.7.10 The
responseXML
attribute
- 4.7.1 The
- 4.8 Events summary
- 5 Interface
FormData
- 6
data:
URLs and HTTP - References
- Acknowledgments
1 Introduction
This section is non-normative.
The XMLHttpRequest
object is the ECMAScript HTTP API. [ECMASCRIPT]
The name of the object is XMLHttpRequest
for compatibility with the Web, though each component of this name is potentially misleading. First, the object supports any text based format, including XML. Second, it can be used to make requests over both HTTP and HTTPS (some implementations support protocols in addition to HTTP and HTTPS, but that functionality is not covered by this specification). Finally, it supports "requests" in a broad sense of the term as it pertains to HTTP; namely all activity involved with HTTP requests or responses for the defined HTTP methods.
Some simple code to do something with data from an XML document fetched over the network:
function processData(data) {
// taking care of data
}
function handler() {
if(this.readyState == this.DONE) {
if(this.status == 200 &&
this.responseXML != null &&
this.responseXML.getElementById('test').textContent) {
// success!
processData(this.responseXML.getElementById('test').textContent);
return;
}
// something went wrong
processData(null);
}
}
var client = new XMLHttpRequest();
client.onreadystatechange = handler;
client.open("GET", "unicorn.xml");
client.send();
If you just want to log a message to the server:
function log(message) {
var client = new XMLHttpRequest();
client.open("POST", "/log");
client.setRequestHeader("Content-Type", "text/plain;charset=UTF-8");
client.send(message);
}
Or if you want to check the status of a document on the server:
function fetchStatus(address) {
var client = new XMLHttpRequest();
client.onreadystatechange = function() {
// in case of network errors this might not give reliable results
if(this.readyState == this.DONE)
returnStatus(this.status);
}
client.open("HEAD", address);
client.send();
}
1.1 Specification history
The XMLHttpRequest
object was initially defined as part of the WHATWG's HTML effort. (Long after Microsoft shipped an implementation.) It moved to the W3C in 2006. Extensions (e.g. progress events and cross-origin requests) to XMLHttpRequest
were developed in a separate draft (XMLHttpRequest Level 2) until end of 2011, at which point the two drafts were merged and XMLHttpRequest
became a single entity again from a standards perspective.
Historical discussion can be found in the following mailing list archives:
2 Conformance
All diagrams, examples, and notes in this specification are non-normative, as are all sections explicitly marked non-normative. Everything else in this specification is normative.
The key words "MUST", "MUST NOT", "REQUIRED", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in the normative parts of this specification are to be interpreted as described in RFC2119. For readability, these words do not appear in all uppercase letters in this specification. [RFC2119]
2.1 Dependencies
This specification relies on several underlying specifications.
-
Cross-Origin Resource Sharing
-
A conforming user agent must support the algorithms of the Cross-Origin Resource Sharing specification. [CORS]
DOM4
-
A conforming user agent must support at least the subset of the functionality defined in DOM4 that this specification relies upon, such as various exceptions and
EventTarget
. [DOM]
DOM Parsing and Serialization
-
A conforming user agent must support at least the serialize concept from DOM Parsing and Serialization. [DOMPS]
Encoding Standard
-
A conforming user agent must support at least the subset of the functionality defined in Encoding Standard that this specification relies upon, such as the
utf-8
encoding
.[ENCODING]
File API
-
A conforming user agent must support at least the subset of the functionality defined in File API that this specification relies upon, such as the
Blob
andFile
interfaces. [FILEAPI]
HTML
-
A conforming user agent must support at least the subset of the functionality defined in HTML that this specification relies upon, such as the basics of the
Window
object and serializing aDocument
object. [HTML]
HTTP
-
A conforming user agent must support some version of the HTTP protocol. Requirements regarding HTTP are made throughout the specification. [HTTP]
Progress Events
-
A conforming user agent must support the Progress Events specification. [PROGRESSEVENTS]
Typed Array
-
A conforming user agent must support the
ArrayBuffer
andArrayBufferView
objects. [TYPEDARRAY]
Web IDL
-
A conforming user agent must also be a conforming implementation of the IDL fragments in this specification, as described in the Web IDL specification. [WEBIDL]
XML
-
A conforming user agent must be a conforming XML processor that reports violations of namespace well-formedness. [XML] [XMLNS]
2.2 Extensibility
User agents, Working Groups, and other interested parties are strongly encouraged to discuss new features on a relevant public forum, preferably pub