Annotation of 2006/webapi/XMLHttpRequest/Overview.src.html, revision 1.249
1.248 avankest 1: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
1.1 avankest 2: <html lang="en-US">
3: <head>
1.207 avankest 4: <title>XMLHttpRequest</title>
1.16 avankest 5: <style type="text/css">
1.117 avankest 6: pre.idl { border:solid thin; background:#eee; color:#000; padding:0.5em }
1.16 avankest 7: pre.idl :link, pre.idl :visited { color:inherit; background:transparent }
1.58 avankest 8: pre code { color:inherit; background:transparent }
1.16 avankest 9: div.example { margin-left:1em; padding-left:1em; border-left:double; color:#222; background:#fcfcfc }
1.89 avankest 10: .note { margin-left:2em; font-weight:bold; font-style:italic; color:#008000 }
1.16 avankest 11: p.note::before { content:"Note: " }
1.203 avankest 12: .XXX { padding:.5em; border:solid #f00 }
13: p.XXX::before { content:"Issue: " }
1.119 avankest 14: dl.switch { padding-left:2em }
1.248 avankest 15: dl.switch > dt { text-indent:-1.5em }
16: dl.switch > dt:before { content:'21円AA'; padding:0 0.5em 0 0; display:inline-block; width:1em; text-align:right; line-height:0.5em }
1.16 avankest 17: em.ct { text-transform:lowercase; font-variant:small-caps; font-style:normal }
18: dfn { font-weight:bold; font-style:normal }
19: code { color:orangered }
20: code :link, code :visited { color:inherit }
1.203 avankest 21: hr:not(.top) { display:block; background:none; border:none; padding:0; margin:2em 0; height:auto }
22: table { border-collapse:collapse; border-style:hidden hidden none hidden }
23: table thead { border-bottom:solid }
24: table tbody th:first-child { border-left:solid }
25: table td, table th { border-left:solid; border-right:solid; border-bottom:solid thin; vertical-align:top; padding:0.2em }
1.16 avankest 26: </style>
1.147 avankest 27: <link rel="stylesheet" href="http://www.w3.org/StyleSheets/TR/W3C-[STATUS]">
1.1 avankest 28: </head>
29: <body>
30: <div class="head">
31: <p><a href="http://www.w3.org/"><img height="48" width="72" alt="W3C" src="http://www.w3.org/Icons/w3c_home"></a></p>
1.12 avankest 32:
1.229 avankest 33: <h1 class="head" id="the-xmlhttprequest-object">XMLHttpRequest</h1>
1.248 avankest 34:
1.12 avankest 35:
1.149 avankest 36: <h2 class="no-num no-toc" id="w3c-doctype">[LONGSTATUS] [DATE: 3 August 2002]</h2>
1.12 avankest 37:
1.1 avankest 38: <dl>
1.10 avankest 39: <dt>This Version:</dt>
1.246 avankest 40: <dd><a href="[VERSION]/">http://www.w3.org/TR/[YEAR]/ED-XMLHttpRequest-[CDATE]/</a></dd>
1.14 avankest 41:
1.10 avankest 42: <dt>Latest Version:</dt>
1.1 avankest 43: <dd><a href="http://www.w3.org/TR/XMLHttpRequest/">http://www.w3.org/TR/XMLHttpRequest/</a></dd>
1.203 avankest 44:
1.189 avankest 45: <dt>Latest Editor Version:</dt>
46: <dd><a href="http://dev.w3.org/2006/webapi/XMLHttpRequest/">http://dev.w3.org/2006/webapi/XMLHttpRequest/</a></dd>
1.14 avankest 47:
1.10 avankest 48: <dt>Previous Versions:</dt>
1.248 avankest 49:
1.231 avankest 50: <dd><a href="http://www.w3.org/TR/2009/WD-XMLHttpRequest-20090820/">http://www.w3.org/TR/2009/WD-XMLHttpRequest-20090820/</a></dd>
1.173 avankest 51: <dd><a href="http://www.w3.org/TR/2008/WD-XMLHttpRequest-20080415/">http://www.w3.org/TR/2008/WD-XMLHttpRequest-20080415/</a></dd>
1.154 avankest 52: <dd><a href="http://www.w3.org/TR/2007/WD-XMLHttpRequest-20071026/">http://www.w3.org/TR/2007/WD-XMLHttpRequest-20071026/</a></dd>
1.133 avankest 53: <dd><a href="http://www.w3.org/TR/2007/WD-XMLHttpRequest-20070618/">http://www.w3.org/TR/2007/WD-XMLHttpRequest-20070618/</a></dd>
1.58 avankest 54: <dd><a href="http://www.w3.org/TR/2007/WD-XMLHttpRequest-20070227/">http://www.w3.org/TR/2007/WD-XMLHttpRequest-20070227/</a></dd>
1.22 avankest 55: <dd><a href="http://www.w3.org/TR/2006/WD-XMLHttpRequest-20060927/">http://www.w3.org/TR/2006/WD-XMLHttpRequest-20060927/</a></dd>
1.1 avankest 56: <dd><a href="http://www.w3.org/TR/2006/WD-XMLHttpRequest-20060619/">http://www.w3.org/TR/2006/WD-XMLHttpRequest-20060619/</a></dd>
57: <dd><a href="http://www.w3.org/TR/2006/WD-XMLHttpRequest-20060405/">http://www.w3.org/TR/2006/WD-XMLHttpRequest-20060405/</a></dd>
1.248 avankest 58:
59:
1.14 avankest 60:
1.1 avankest 61: <dt>Editor:</dt>
62: <dd><a href="http://annevankesteren.nl/">Anne van Kesteren</a>
63: (<a href="http://www.opera.com/">Opera Software ASA</a>)
64: <<a href="mailto:annevk@opera.com">annevk@opera.com</a>></dd>
65: </dl>
1.14 avankest 66:
1.1 avankest 67: <p class="copyright"><a href="http://www.w3.org/Consortium/Legal/ipr-notice#Copyright">Copyright</a>
1.229 avankest 68: © 2009
1.51 avankest 69: <a href="http://www.w3.org/"><acronym title="World Wide Web Consortium">W3C</acronym></a><sup>®</sup>
70: (<a href="http://www.csail.mit.edu/"><acronym title="Massachusetts Institute of Technology">MIT</acronym></a>,
71: <a href="http://www.ercim.org/"><acronym title="European Research Consortium for Informatics and Mathematics">ERCIM</acronym></a>,
72: <a href="http://www.keio.ac.jp/">Keio</a>), All Rights Reserved. W3C
73: <a href="http://www.w3.org/Consortium/Legal/ipr-notice#Legal_Disclaimer">liability</a>,
74: <a href="http://www.w3.org/Consortium/Legal/ipr-notice#W3C_Trademarks">trademark</a>
75: and
76: <a href="http://www.w3.org/Consortium/Legal/copyright-documents">document
77: use</a> rules apply.</p>
1.1 avankest 78: </div>
1.12 avankest 79:
1.1 avankest 80: <hr>
1.12 avankest 81:
1.1 avankest 82: <h2 class="no-num no-toc" id="specabstract">Abstract</h2>
1.10 avankest 83:
1.248 avankest 84:
85:
1.229 avankest 86: <p>The XMLHttpRequest specification defines an API
1.207 avankest 87: that provides scripted client functionality for transferring data between
88: a client and a server.</p>
1.248 avankest 89:
1.12 avankest 90:
1.1 avankest 91: <h2 class="no-num no-toc" id="sotd">Status of this Document</h2>
1.12 avankest 92:
1.1 avankest 93: <p><em>This section describes the status of this document at the time of its
1.36 avankest 94: publication. Other documents may supersede this document. A list of current
95: W3C publications and the latest revision of this technical report can be
96: found in the <a href="http://www.w3.org/TR/">W3C technical reports index</a>
1.117 avankest 97: at http://www.w3.org/TR/.</em></p>
1.36 avankest 98:
1.248 avankest 99: <p>This is the [DATE: 3 August 2002] <!--Last Call Working Draft-->[LONGSTATUS] of
100: XMLHttpRequest. Please send comments to
101: <a href="mailto:public-webapps@w3.org?subject=[XHR]%20">public-webapps@w3.org</a>
1.207 avankest 102: (<a href="http://lists.w3.org/Archives/Public/public-webapps/">archived</a>)
1.248 avankest 103: with <samp>[XHR]</samp> at the start of the subject line.</p>
1.51 avankest 104:
1.47 avankest 105: <p>This document is produced by the
1.248 avankest 106: <a href="http://www.w3.org/2008/webapps/">Web Applications</a> (WebApps) Working Group.
107: The WebApps Working Group is part of the
1.207 avankest 108: <a href="http://www.w3.org/2006/rwc/Activity">Rich Web Clients Activity</a>
1.248 avankest 109: in the W3C <a href="http://www.w3.org/Interaction/">Interaction Domain</a>.</p>
1.12 avankest 110:
1.1 avankest 111: <p>This document was produced by a group operating under the
1.248 avankest 112: <a href="http://www.w3.org/Consortium/Patent-Policy-20040205/">5 February 2004
113: W3C Patent Policy</a>. W3C maintains a
1.207 avankest 114: <a rel="disclosure" href="http://www.w3.org/2004/01/pp-impl/42538/status">public
1.248 avankest 115: list of any patent disclosures</a> made in connection with the deliverables of
116: the group; that page also includes instructions for disclosing a patent. An
117: individual who has actual knowledge of a patent which the individual believes
118: contains
1.36 avankest 119: <a href="http://www.w3.org/Consortium/Patent-Policy-20040205/#def-essential">Essential
120: Claim(s)</a> must disclose the information in accordance with
121: <a href="http://www.w3.org/Consortium/Patent-Policy-20040205/#sec-Disclosure">section
122: 6 of the W3C Patent Policy</a>.</p>
1.12 avankest 123:
1.248 avankest 124: <p>Publication as a Working Draft does not imply endorsement by the W3C
125: Membership. This is a draft document and may be updated, replaced or
126: obsoleted by other documents at any time. It is inappropriate to cite this
127: document as other than work in progress.</p>
1.207 avankest 128:
129:
1.1 avankest 130: <h2 class="no-num no-toc" id="toc">Table of Contents</h2>
1.21 avankest 131:
1.1 avankest 132: <!--toc-->
1.12 avankest 133:
1.207 avankest 134:
135:
1.1 avankest 136: <h2 id="introduction">Introduction</h2>
1.12 avankest 137:
1.1 avankest 138: <p><em>This section is non-normative.</em></p>
1.12 avankest 139:
1.183 avankest 140: <p>The <code>XMLHttpRequest</code> object implements an interface exposed
141: by a scripting engine that allows scripts to perform HTTP client
142: functionality, such as submitting form data or loading data from a
143: server. It is the ECMAScript HTTP API.</p>
1.24 avankest 144:
1.1 avankest 145: <p>The name of the object is <code>XMLHttpRequest</code> for compatibility
1.183 avankest 146: with the Web, though each component of this name is potentially
147: misleading. First, the object supports any text based format, including
148: XML. Second, it can be used to make requests over both HTTP and HTTPS
149: (some implementations support protocols in addition to HTTP and HTTPS, but
150: that functionality is not covered by this specification). Finally, it
151: supports "requests" in a broad sense of the term as it pertains to HTTP;
152: namely all activity involved with HTTP requests or responses for the
153: defined HTTP methods.</p>
1.12 avankest 154:
1.14 avankest 155: <div class="example">
156: <p>Some simple code to do something with data from an XML document fetched
1.22 avankest 157: over the network:</p>
1.16 avankest 158:
1.58 avankest 159: <pre><code>function test(data) {
1.14 avankest 160: // taking care of data
161: }
162:
163: function handler() {
1.117 avankest 164: if(this.readyState == 4 && this.status == 200) {
1.14 avankest 165: // so far so good
1.117 avankest 166: if(this.responseXML != null && this.responseXML.getElementById('test').firstChild.data)
167: // success!
1.14 avankest 168: test(this.responseXML.getElementById('test').firstChild.data);
169: else
170: test(null);
1.117 avankest 171: } else if (this.readyState == 4 && this.status != 200) {
1.14 avankest 172: // fetched the wrong page or network error...
173: test(null);
174: }
175: }
1.12 avankest 176:
1.14 avankest 177: var client = new XMLHttpRequest();
178: client.onreadystatechange = handler;
179: client.open("GET", "test.xml");
1.58 avankest 180: client.send();</code></pre>
1.16 avankest 181:
1.56 avankest 182: <p>If you just want to log a message to the server:</p>
1.16 avankest 183:
1.58 avankest 184: <pre><code>function log(message) {
1.14 avankest 185: var client = new XMLHttpRequest();
1.56 avankest 186: client.open("POST", "/log");
1.57 avankest 187: client.setRequestHeader("Content-Type", "text/plain;charset=UTF-8");
1.14 avankest 188: client.send(message);
1.58 avankest 189: }</code></pre>
1.16 avankest 190:
1.14 avankest 191: <p>Or if you want to check the status of a document on the server:</p>
1.16 avankest 192:
1.58 avankest 193: <pre><code>function fetchStatus(address) {
1.14 avankest 194: var client = new XMLHttpRequest();
195: client.onreadystatechange = function() {
196: // in case of network errors this might not give reliable results
197: if(this.readyState == 4)
198: returnStatus(this.status);
199: }
200: client.open("HEAD", address);
201: client.send();
1.58 avankest 202: }</code></pre>
1.14 avankest 203: </div>
1.12 avankest 204:
1.203 avankest 205:
206:
1.248 avankest 207:
208: <h2 id="conformance">Conformance Criteria</h2>
1.12 avankest 209:
1.27 avankest 210: <p>Everything in this specification is normative except for diagrams,
211: examples, notes and sections marked non-normative.</p>
1.12 avankest 212:
1.1 avankest 213: <p>The key words <em class="ct">must</em>, <em class="ct">must not</em>,
1.74 avankest 214: <em class="ct">should</em> and <em class="ct">may</em> in this document are
215: to be interpreted as described in RFC 2119.
216: [<cite><span>RFC2119</span></cite>]</p>
1.12 avankest 217:
1.1 avankest 218: <p>This specification defines the following classes of products:</p>
1.12 avankest 219:
1.1 avankest 220: <dl>
1.74 avankest 221: <dt><dfn id="conforming-user-agent">Conforming user agent</dfn></dt>
1.24 avankest 222:
1.74 avankest 223: <dd>
224: <p>A user agent <em class="ct">must</em> behave as described in this
1.106 avankest 225: specification in order to be considered conformant.</p>
1.95 avankest 226:
1.248 avankest 227:
1.224 avankest 228: <p>If the user agent is not a <span>conforming XML user agent</span> the
229: <span>XML response entity body</span> <em class="ct">must</em> (always)
1.239 avankest 230: be null.</p>
1.248 avankest 231:
1.140 avankest 232:
233: <p>User agents <em class="ct">may</em> implement algorithms given in
234: this specification in any way desired, so long as the end result is
235: indistinguishable from the result that would be obtained by the
236: specification's algorithms.</p>
1.110 avankest 237:
1.95 avankest 238: <p class="note">This specification uses both the terms "conforming user
239: agent(s)" and "user agent(s)" to refer to this product class.</p>
1.74 avankest 240: </dd>
1.248 avankest 241:
1.94 avankest 242: <dt><dfn id="conforming-xml-user-agent">Conforming XML user agent</dfn></dt>
1.110 avankest 243:
1.224 avankest 244: <dd><p>An XML user agent <em class="ct">must</em> be a
245: <span>conforming user agent</span> and <em class="ct">must</em> be a
246: conforming XML processor that reports violations of
247: namespace well-formedness. [<cite><span>XML</span></cite>]</p></dd>
1.248 avankest 248:
1.1 avankest 249: </dl>
1.32 avankest 250:
1.153 avankest 251: <h3 id="dependencies">Dependencies</h3>
1.33 avankest 252:
1.29 avankest 253: <p>This specification relies on several underlying specifications.</p>
1.248 avankest 254:
1.29 avankest 255: <dl>
1.248 avankest 256:
257:
1.29 avankest 258: <dt>DOM</dt>
1.33 avankest 259:
1.155 avankest 260: <dd><p>A <span title="conforming user agent">conforming user agent</span>
1.176 avankest 261: <em class="ct">must</em> support at least the subset of the functionality
1.182 avankest 262: defined in DOM Events and DOM Core that this specification relies upon,
263: such as various exceptions and <code>EventTarget</code>.
1.155 avankest 264: [<cite><span>DOM2Events</span></cite>]
265: [<cite><span>DOM3Core</span></cite>]</p></dd>
1.29 avankest 266:
1.161 avankest 267: <dt>HTML 5</dt>
268:
269: <dd>
1.189 avankest 270: <p>A <span>conforming user agent</span> <em class="ct">must</em> support
271: at least the subset of the functionality defined in HTML 5 that
1.197 avankest 272: this specification relies upon, such as the basics of the
273: <code>Window</code> object and serializing a <code>Document</code>
274: object. [<cite><span>HTML5</span></cite>]</p>
1.161 avankest 275:
276: <p class="note">The
277: <a href="http://www.w3.org/TR/2006/WD-Window-20060407/">Window Object
278: 1.0</a> draft is not referenced normatively as it appears to be no
279: longer maintained and HTML 5 defines the <code>Window</code> object
280: in more detail. This specification already depends on HTML 5 for
281: other reasons so there is not much additional overhead because of
282: this.</p>
283:
1.29 avankest 284: <dt>HTTP</dt>
1.33 avankest 285:
1.248 avankest 286: <dd><p>A <span title="conforming user agent">conforming user agent</span>
287: <em class="ct">must</em> support some version of the HTTP protocol.
288: Requirements regarding HTTP are made throughout the specification.
289: [<cite><span>RFC2616</span></cite>]</p></dd>
1.189 avankest 290:
1.181 avankest 291: <dt>Web IDL</dt>
1.203 avankest 292:
1.248 avankest 293: <dd><p>A <span>conforming user agent</span> <em class="ct">must</em> also
294: be a conforming implementation of the IDL fragments in this
295: specification, as described in the Web IDL specification.
1.181 avankest 296: [<cite><span>WebIDL</span></cite>]
1.248 avankest 297:
298:
1.29 avankest 299: </dl>
1.12 avankest 300:
1.157 avankest 301:
1.153 avankest 302: <h3 id="terminology">Terminology</h3>
1.209 avankest 303:
304: <p>Comparing two strings in a
305: <dfn id="case-sensitive">case-sensitive</dfn> manner means comparing them
306: exactly, codepoint for codepoint.</p>
307:
308: <p>Comparing two strings in an
309: <dfn id="ascii-case-insensitive">ASCII case-insensitive</dfn> manner means
310: comparing them exactly, codepoint for codepoint, except that the
311: characters in the range U+0041 .. U+005A (i.e. LATIN CAPITAL LETTER A to
312: LATIN CAPITAL LETTER Z) and the corresponding characters in the range
313: U+0061 .. U+007A (i.e. LATIN SMALL LETTER A to LATIN SMALL LETTER Z) are
314: considered to also match.</p>
315:
316: <p><dfn id="converted-to-ascii-upercase" title="converted to ASCII uppercase">Converting a string to ASCII uppercase</dfn>
317: means replacing all characters in the range U+0061 .. U+007A (i.e.
318: LATIN SMALL LETTER A to LATIN SMALL LETTER Z) with the corresponding
319: characters in the range U+0041 .. U+005A (i.e. LATIN CAPITAL LETTER A to
320: LATIN CAPITAL LETTER Z).</p>
1.12 avankest 321:
1.233 avankest 322: <p><dfn id="dfn-obtain-unicode">convert a DOMString to a sequence of Unicode characters</dfn>
323: is defined by the Web IDL specification.
324: [<cite><span>WebIDL</span></cite>]</p>
1.248 avankest 325:
326:
1.233 avankest 327:
1.200 avankest 328: <p>The terms and algorithms
329: <dfn id="url-fragment"><fragment></dfn>,
330: <dfn id="url-scheme"><scheme></dfn>,
1.203 avankest 331: <dfn id="document-base-url">document base URL</dfn>,
1.244 avankest 332: <dfn id="document-character-encoding">document's character encoding</dfn>,
1.205 avankest 333: <dfn id="event-handler-attributes-0">event handler attributes</dfn>,
1.203 avankest 334: <dfn id="event-handler-event-type">event handler event type</dfn>,
335: <dfn id="fully-active">fully active</dfn>,
1.206 avankest 336: <dfn id="function"><code>Function</code></dfn>,
1.225 avankest 337: <dfn id="dom-innerhtml" title="dom-innerHTML"><code>innerHTML</code></dfn>,
1.200 avankest 338: <dfn id="origin">origin</dfn>,
1.232 avankest 339: <dfn id="preferred-mime-name">preferred MIME name</dfn>,
1.203 avankest 340: <dfn id="resolve-a-url">resolve a URL</dfn>,
341: <dfn id="same-origin">same origin</dfn>,
342: <dfn id="storage-mutex">storage mutex</dfn>,
343: <dfn id="task">task</dfn>,
344: <dfn id="task-source">task source</dfn>,
345: <dfn id="url">URL</dfn>,
1.232 avankest 346: <dfn id="url-character-encoding">URL character encoding</dfn>,
347: <dfn id="queue-a-task">queue a task</dfn>, and
348: <dfn id="valid-mime-type">valid MIME type</dfn>
1.200 avankest 349: are defined by the HTML 5 specification.
1.157 avankest 350: [<cite><span>HTML5</span></cite>]</p>
1.210 avankest 351:
1.203 avankest 352: <p>The term <dfn id="entity-body">entity body</dfn> is used as described
353: in RFC 2616. <dfn id="method-token">Method token</dfn> is used as
354: described in section 5.1.1 of RFC 2616. <dfn><code>field-name</code></dfn>
355: and <dfn><code>field-value</code></dfn> are used as described in section
356: 4.2 of RFC 2616. [<cite><span>RFC2616</span></cite>]</p>
1.218 avankest 357:
1.203 avankest 358: <p><dfn><code>userinfo</code></dfn> is used as described in section 3.2.1
359: of RFC 3986. [<cite><span>RFC3986</span></cite>]</p>
360:
1.248 avankest 361:
362:
1.203 avankest 363: <p>To <dfn id="dispatch-readystatechange-event">dispatch a
364: <code>readystatechange</code> event</dfn> means that an event with the
1.232 avankest 365: name <code>readystatechange</code>, which does not bubble and is not
366: cancelable, and which uses the <code>Event</code> interface, is to be
367: dispatched at the <code>XMLHttpRequest</code> object.</p>
1.80 avankest 368:
1.248 avankest 369:
370:
1.153 avankest 371: <h3 id="extensibility">Extensibility</h3>
1.12 avankest 372:
1.81 avankest 373: <p>Extensions of the API defined by this specification are <em>strongly
1.22 avankest 374: discouraged</em>. User agents, Working Groups and other interested parties
1.33 avankest 375: should discuss extensions on a relevant public forum, preferably
1.224 avankest 376: <a href="mailto:public-webapps@w3.org">public-webapps@w3.org</a>.</p>
1.12 avankest 377:
1.153 avankest 378:
1.223 avankest 379: <h2>The <code title="">XMLHttpRequest</code> Interface</h2>
1.12 avankest 380:
1.58 avankest 381: <p>The <code>XMLHttpRequest</code> object can be used by scripts to
1.22 avankest 382: programmatically connect to their originating server via HTTP.</p>
1.12 avankest 383:
1.228 avankest 384: <pre class="idl">[NoInterfaceObject]
385: interface <dfn id="xmlhttprequesteventtarget">XMLHttpRequestEventTarget</dfn> : EventTarget {
386: // for future use
387: };
388:
1.240 avankest 389: [<span title="dom-XMLHttpRequest">Constructor</span>]
1.228 avankest 390: interface <dfn id="xmlhttprequest">XMLHttpRequest</dfn> : <span>XMLHttpRequestEventTarget</span> {
1.206 avankest 391: // <a href="#event-handler-attributes">event handler attributes</a>
392: attribute <span>Function</span> <span>onreadystatechange</span>;
1.58 avankest 393:
1.205 avankest 394: // <a href="#states">states</a>
1.71 avankest 395: const unsigned short <span title="UNSENT state">UNSENT</span> = 0;
1.134 avankest 396: const unsigned short <span title="OPENED state">OPENED</span> = 1;
1.123 avankest 397: const unsigned short <span title="HEADERS_RECEIVED state">HEADERS_RECEIVED</span> = 2;
1.86 avankest 398: const unsigned short <span title="LOADING state">LOADING</span> = 3;
1.117 avankest 399: const unsigned short <span title="DONE state">DONE</span> = 4;
1.70 avankest 400: readonly attribute unsigned short <span>readyState</span>;
1.58 avankest 401:
1.205 avankest 402: // <a href="#request">request</a>
1.226 avankest 403: void <span>open</span>(DOMString <var>method</var>, DOMString <var title="">url</var>);
404: void <span>open</span>(DOMString <var>method</var>, DOMString <var title="">url</var>, boolean <var>async</var>);
1.228 avankest 405: void <span>open</span>(DOMString <var>method</var>, DOMString <var title="">url</var>, boolean <var>async</var>, DOMString? <var>user</var>);
406: void <span>open</span>(DOMString <var>method</var>, DOMString <var title="">url</var>, boolean <var>async</var>, DOMString? <var>user</var>, DOMString? <var>password</var>);
1.226 avankest 407: void <span>setRequestHeader</span>(DOMString <var>header</var>, DOMString <var>value</var>);
1.70 avankest 408: void <span>send</span>();
1.226 avankest 409: void <span>send</span>(Document <var>data</var>);
1.228 avankest 410: void <span>send</span>([AllowAny] DOMString? <var>data</var>);
1.70 avankest 411: void <span>abort</span>();
1.58 avankest 412:
1.205 avankest 413: // <a href="#response">response</a>
1.203 avankest 414: readonly attribute unsigned short <span>status</span>;
415: readonly attribute DOMString <span>statusText</span>;
1.226 avankest 416: DOMString <span>getResponseHeader</span>(DOMString <var>header</var>);
1.70 avankest 417: DOMString <span>getAllResponseHeaders</span>();
418: readonly attribute DOMString <span>responseText</span>;
419: readonly attribute Document <span>responseXML</span>;
1.228 avankest 420: };</pre>
1.8 avankest 421:
1.235 avankest 422: <!-- XXX domintro boxes; HTML5-style -->
423:
1.124 avankest 424:
1.203 avankest 425: <h3>Origin and Base URL</h3>
426:
427: <p>Each <code>XMLHttpRequest</code> object has an associated
428: <dfn id="xmlhttprequest-origin"><code>XMLHttpRequest</code> origin</dfn>
429: and an
430: <dfn id="xmlhttprequest-base-url"><code>XMLHttpRequest</code> base URL</dfn>.
431:
432: <p>This specification defines their values when the global object is
433: represented by the <code>Window</code> object. When the
1.246 avankest 434: <code>XMLHttpRequest</code> object is used in other contexts their values
1.203 avankest 435: will have to be defined as appropriate for that context. That is
436: considered to be out of scope for this specification.</p>
437:
438: <hr>
439:
440: <p>In environments where the global object is represented by the
441: <code>Window</code> object the <code>XMLHttpRequest</code> object has an
442: associated
443: <dfn id="xmlhttprequest-document"><code>XMLHttpRequest</code> <code>Document</code></dfn>
444: which is the <code>Document</code> object associated with the
445: <code>Window</code> object for which the <code>XMLHttpRequest</code>
446: interface object was created.</p>
447:
448: <p class="note">The
449: <span><code>XMLHttpRequest</code> <code>Document</code></span> is used to
450: determine the <span><code>XMLHttpRequest</code> origin</span> and
451: <span><code>XMLHttpRequest</code> base URL</span> at a later stage.</p>
452:
453:
454: <h3>Task Sources</h3>
1.218 avankest 455:
1.203 avankest 456: <p>The following <span title="task source">task sources</span> are used by
457: this specification:</p>
1.218 avankest 458:
1.203 avankest 459: <dl>
460: <dt>The <dfn><code>XMLHttpRequest</code> event task source</dfn></dt>
1.218 avankest 461:
1.203 avankest 462: <dd>This <span>task source</span> is used for events that are to be
463: asynchronously dispatched.</dd>
1.218 avankest 464:
1.203 avankest 465: <dt>The <dfn><code>XMLHttpRequest</code> networking task source</dfn></dt>
1.218 avankest 466:
1.203 avankest 467: <dd>This <span>task source</span> is used for network activity.</dd>
468: </dl>
1.218 avankest 469:
1.203 avankest 470: <p>Unless otherwise stated the <span>task source</span> used for all tasks
471: <span title="queue a task">queued</span> in this specification is the
472: <span><code>XMLHttpRequest</code> event task source</span>.</p>
473:
474:
1.211 avankest 475: <h3 id="constructors">Constructors</h3>
1.203 avankest 476:
477: <p>When the
1.240 avankest 478: <dfn id="dom-xmlhttprequest" title="dom-XMLHttpRequest"><code title="">XMLHttpRequest()</code></dfn>
1.203 avankest 479: constructor is invoked, the user agent <em class="ct">must</em> return a
480: new <code>XMLHttpRequest</code> object.</p>
481:
482:
1.205 avankest 483: <h3 id="event-handler-attributes">Event Handler Attributes</h3>
484:
1.248 avankest 485:
486:
1.205 avankest 487: <p>The following is the
488: <span title="event handler attributes">event handler attribute</span> (and
489: its corresponding <span>event handler event type</span>) that
490: <em class="ct">must</em> be supported as DOM attribute by the
491: <code>XMLHttpRequest</code> object:</p>
492:
493: <table>
494: <thead>
495: <tr>
496: <th><span title="event handler attributes">event handler attribute</span>
497: <th><span>event handler event type</span>
498: <tbody>
499: <tr>
500: <td><dfn id="onreadystatechange"><code>onreadystatechange</code></dfn>
501: <td><code>readystatechange</code></td>
502: </table>
503:
504:
505: <h3 id="states">States</h3>
1.203 avankest 506:
507: <p>The <code>XMLHttpRequest</code> object can be in several states. The
508: <dfn id="readystate"><code>readyState</code></dfn> attribute, on getting,
509: <em class="ct">must</em> return the current state, which
510: <em class="ct">must</em> be one of the following values:</p>
511:
512: <dl>
513: <dt><dfn id="unsent-state" title="UNSENT state"><code>UNSENT</code></dfn>
514: (numeric value 0)</dt>
515: <dd><p>The object has been constructed.</p></dd>
516:
517: <dt><dfn id="opened-state" title="OPENED state"><code>OPENED</code></dfn>
518: (numeric value 1)</dt>
519: <dd><p>The <code>open()</code> method has been successfully invoked.
520: During this state request headers can be set using
521: <code>setRequestHeader()</code> and the request can be made using the
522: <code>send()</code> method.</p></dd>
523:
524: <dt><dfn id="headers-received-state" title="HEADERS_RECEIVED state"><code>HEADERS_RECEIVED</code></dfn>
525: (numeric value 2)</dt>
526: <dd><p>All HTTP headers have been received. Several response members of
527: the object are now available.</p></dd>
528:
529: <dt><dfn id="loading-state" title="LOADING state"><code>LOADING</code></dfn>
530: (numeric value 3)</dt>
531: <dd><p>The <span>response entity body</span> is being received.</p></dd>
532:
533: <dt><dfn id="done-state" title="DONE state"><code>DONE</code></dfn>
534: (numeric value 4)</dt>
535: <dd><p>The data transfer has been completed or something went wrong
536: during the transfer (e.g. infinite redirects).</p></dd>
537: </dl>
538:
539: <p>The <span title="OPENED state">OPENED</span> state has an associated
540: <dfn id="send-flag"><code>send()</code> flag</dfn> that indicates whether
541: the <code>send()</code> method has been invoked. It can be either true
542: or false and has an initial value of false.</p>
543:
544: <p>The <span title="DONE state">DONE</span> state has an associated
545: <dfn id="error-flag">error flag</dfn> that indicates some type of network
546: error or abortion. It can be either true or false and has an initial value
547: of false.</p>
548:
549:
1.205 avankest 550: <h3 id="request">Request</h3>
1.203 avankest 551:
552: <p>The <code>XMLHttpRequest</code> object holds the following request
553: metadata variables:</p>
554:
1.204 avankest 555: <dl>
556: <dt>The <dfn>asynchronous flag</dfn></dt>
557: <dd>A flag that is either true or false that indicates whether the
558: request is done asynchronously.</dd>
559:
560: <dt>The <dfn>request method</dfn></dt>
561: <dd>The method used in the request.</dd>
562:
563: <dt>The <dfn>request URL</dfn></dt>
564: <dd>The <span>URL</span> used in the request.</dd>
1.218 avankest 565:
1.204 avankest 566: <dt>The <dfn>request username</dfn></dt>
567: <dd>The username used in the request or null if there is no
568: username.</dd>
1.218 avankest 569:
1.204 avankest 570: <dt>The <dfn>request password</dfn></dt>
571: <dd>The password used in the request or null if there is no
572: password.</dd>
1.218 avankest 573:
1.204 avankest 574: <dt>The <dfn id="author-request-headers">author request headers</dfn></dt>
575: <dd>A list consisting of HTTP header name/value pairs to be used in the
576: request.</p>
1.203 avankest 577:
1.204 avankest 578: <dt>The <dfn>request entity body</dfn></dt>
1.218 avankest 579: <dd>The <span>entity body</span> used in the request.</dd>
1.248 avankest 580:
581:
1.204 avankest 582: </dl>
1.203 avankest 583:
1.248 avankest 584:
585:
1.203 avankest 586:
587: <h4>The <code title="">open()</code> method</h4>
1.88 avankest 588:
1.203 avankest 589: <p>When the <dfn id="open" title="open"><code>open(<var>method</var>,
590: <var title="">url</var>, <var>async</var>, <var>user</var>,
591: <var>password</var>)</code></dfn> method is invoked, the user agent
1.217 avankest 592: <em class="ct">must</em> run these steps (unless otherwise indicated):</p>
1.90 avankest 593:
1.88 avankest 594: <ol>
1.203 avankest 595: <li>
596: <p>If the <code>XMLHttpRequest</code> object has an associated
597: <span><code>XMLHttpRequest</code> <code>Document</code></span> run
598: these substeps:</p>
1.90 avankest 599:
1.203 avankest 600: <ol>
601: <li><p>If the
602: <span><code>XMLHttpRequest</code> <code>Document</code></span> is not
603: <span>fully active</span> raise an <code>INVALID_STATE_ERR</code>
604: exception and terminate the overall set of steps.</p></li>
605:
606: <li><p>Let <span><code>XMLHttpRequest</code> base URL</span> be the
607: <span>document base URL</span> of the
608: <span><code>XMLHttpRequest</code> <code>Document</code></span>.</p></li>
609:
610: <li><p>Let <span><code>XMLHttpRequest</code> origin</span> be the
611: <span>origin</span> of the
612: <span><code>XMLHttpRequest</code> <code>Document</code></span>.</p></li>
613: </ol>
614: </li>
1.90 avankest 615:
1.203 avankest 616: <li><p>If <var>method</var> does not match the
1.210 avankest 617: <span>Method token</span> production raise a <code>SYNTAX_ERR</code>
618: exception and terminate these steps.</p></li>
1.118 avankest 619:
1.115 avankest 620: <li>
1.203 avankest 621: <p>If <var>method</var> is an
622: <span>ASCII case-insensitive</span> match for
623: <code>CONNECT</code>, <code>DELETE</code>, <code>GET</code>,
624: <code>HEAD</code>, <code>OPTIONS</code>, <code>POST</code>,
625: <code>PUT</code>, <code>TRACE</code>, or <code>TRACK</code>
1.209 avankest 626: <span title="converted to ASCII uppercase">convert <var>method</var> to ASCII uppercase</span>.</p>
1.118 avankest 627:
1.203 avankest 628: <p class="note">If it does not match any of the above, it is passed
629: through <em>literally</em>, including in the final request.</p>
1.115 avankest 630: </li>
1.203 avankest 631: <!-- WebKit (and supposedly Gecko) also uppercase: COPY, INDEX, LOCK,
632: M-POST, MKCOL, MOVE, PROPFIND, PROPPATCH, and UNLOCK. -->
1.118 avankest 633:
1.111 avankest 634: <li>
1.209 avankest 635: <p>If <var>method</var> is a <span>case-sensitive</span> match for
636: <code>CONNECT</code>, <code>TRACE</code>, or <code>TRACK</code> the
637: user agent <em class="ct">should</em> raise a <code>SECURITY_ERR</code>
638: exception and terminate these steps.</p>
1.117 avankest 639:
1.203 avankest 640: <p class="note">Allowing these methods poses a security risk.
641: [<cite><span>HTTPVERBSEC</span></cite>]</p>
1.111 avankest 642: </li>
643:
1.203 avankest 644: <li><p>Let <var title="">url</var> be a <span>URL</span>.</p></li>
645:
1.209 avankest 646: <li><p>Let <span>URL character encoding</span> of <var title="">url</var>
647: be UTF-8.</p></li>
1.113 avankest 648:
1.203 avankest 649: <li><p><span title="Resolve a URL">Resolve <var title="">url</var></span>
650: relative to the <span><code>XMLHttpRequest</code> base URL</span>.
651: If the algorithm returns an error raise a <code>SYNTAX_ERR</code>
652: exception and terminate these steps.</p></li>
653:
654: <!-- Presto and Gecko override the encoding. WebKit does not. Trident
655: does not support non-ASCII URLs. This matters for the <query> component,
656: see HTML5. -->
657:
658: <li><p>Drop <code><fragment></code> from <var title="">url</var>.</p></li>
659:
660: <li><p>If <var title="">url</var> contains an unsupported <code><scheme></code>
661: raise a <code>NOT_SUPPORTED_ERR</code> and terminate these
662: steps.</p></li>
663:
664: <li><p>If the <code>"user:password"</code> format in the
665: <code>userinfo</code> production is not supported for the relevant scheme
666: and <var title="">url</var> contains this format raise a
667: <code>SYNTAX_ERR</code> and terminate these steps.</p></li>
668:
669: <li><p>If <var title="">url</var> contains the <code>"user:password"</code>
670: format let <var>temp user</var> be the user part and
671: <var>temp password</var> be the password part.</p></li>
672:
673: <li><p>If <var title="">url</var> just contains the <code>"user"</code>
674: format let <var>temp user</var> be the user part.</p></li>
675:
1.248 avankest 676:
1.203 avankest 677: <li><p>If the <span>origin</span> of <var title="">url</var> is not
678: <span>same origin</span> with the
679: <span><code>XMLHttpRequest</code> origin</span> the user agent
680: <em class="ct">should</em> raise a <code>SECURITY_ERR</code> exception
681: and terminate these steps.</p></li>
1.248 avankest 682:
683:
1.203 avankest 684:
1.211 avankest 685: <li><p>Let <var>async</var> be the value of the <var>async</var> argument
686: or <code>true</code> if it was omitted.</p></li>
1.203 avankest 687:
688: <li>
689: <p>If the <var>user</var> argument was not omitted follow these sub
690: steps:</p>
1.90 avankest 691:
1.203 avankest 692: <ol>
693: <li><p>If the syntax of <var>user</var> does not match the syntax
694: specified by the relevant authentication scheme, raise a
695: <code>SYNTAX_ERR</code> exception and terminate the overall set of
696: steps.</p></li>
1.163 avankest 697:
1.239 avankest 698: <li><p>If <var>user</var> is null let <var>temp user</var>
1.203 avankest 699: be null.</p></li>
1.90 avankest 700:
1.203 avankest 701: <li><p>Otherwise let <var>temp user</var> be <var>user</var>.</p></li>
702: </ol>
1.88 avankest 703:
1.203 avankest 704: <p class="note">These steps override anything that may have been set by
705: the <var title="">url</var> argument.</p>
706: </li>
1.88 avankest 707:
1.128 avankest 708: <li>
1.203 avankest 709: <p>If the <var>password</var> argument was not omitted follow these sub
710: steps:</p>
711:
712: <ol>
713: <li><p>If the syntax of <var>password</var> does not match the syntax
714: specified by the relevant authentication scheme, raise a
715: <code>SYNTAX_ERR</code> exception and terminate the overall set of
716: steps.</p></li>
1.167 avankest 717:
1.239 avankest 718: <li><p>If <var>password</var> is null let
1.203 avankest 719: <var>temp password</var> be null.</p></li>
720:
721: <li><p>Otherwise let <var>temp password</var> be
722: <var>password</var>.</p></li>
723: </ol>
724:
725: <p class="note">These steps override anything that may have been set by
726: the <var title="">url</var> argument.</p>
1.128 avankest 727: </li>
1.88 avankest 728:
1.203 avankest 729: <li><p><span title="abort send()">Abort the <code>send()</code> algorithm</span>.</p></li>
730:
731: <li><p>The user agent <em class="ct">should</em> cancel any network
732: activity for which the object is responsible.</p></li>
733: <!-- we can hardly require it... -->
734:
735: <li>
736: <p>Set variables associated with the object as follows:</p>
737:
738: <ul>
739: <li><p>Set the <span><code>send()</code> flag</span> to false.</p></li>
740:
741: <li><p>Set <span>response entity body</span> to null.</p></li>
742:
743: <li><p>Empty the list of <span>author request headers</span>.</p>
744:
745: <li><p>Set the <span>request method</span> to <var>method</var>.</p></li>
746:
747: <li><p>Set the <span>request URL</span> to <var title="">url</var>.</p></li>
1.58 avankest 748:
1.203 avankest 749: <li><p>Set the <span>request username</span> to <var>temp user</var>.</p></li>
1.12 avankest 750:
1.203 avankest 751: <li><p>Set the <span>request password</span> to <var>temp password</var>.</p></li>
1.12 avankest 752:
1.203 avankest 753: <li><p>Set the <span>asynchronous flag</span> to true if <var>async</var> is
754: <code>true</code>. Otherwise set it to false.</p></li>
755: </ul>
756: </li>
1.58 avankest 757:
1.203 avankest 758: <li><p>Switch the the state to
759: <span title="OPENED state">OPENED</span>.</p></li>
1.58 avankest 760:
1.203 avankest 761: <li><p><span>Dispatch a <code>readystatechange</code> event</span>.</p></li>
762: </ol>
1.248 avankest 763:
764:
1.203 avankest 765: <p class="note">A future version or extension of this specification will
766: define a way of doing cross-origin requests.</p>
1.248 avankest 767:
1.58 avankest 768:
1.14 avankest 769:
1.203 avankest 770: <h4>The <code title="">setRequestHeader()</code> method</h4>
1.12 avankest 771:
1.235 avankest 772: <!-- XXX domintro authors
1.203 avankest 773: The <code>setRequestHeader()</code> method can be used to set new request
1.184 avankest 774: headers and append to request headers already in the list.</p>
1.203 avankest 775: -->
1.45 avankest 776:
1.203 avankest 777: <p>As indicated in the algorithm below certain headers cannot be set and
778: are left up to the user agent. In addition there are certain other headers
779: the user agent will take control of if they are not set by the author as
780: indicated at the end of the <code>send()</code> method section.</p>
781:
782: <p class="note">The <code>setRequestHeader()</code> method appends a
783: value if the HTTP header given as argument is already part of the
784: <span>author request headers</span> list.</p>
785:
1.248 avankest 786:
787:
1.203 avankest 788: <p>When the
789: <dfn id="setrequestheader" title="setrequestheader"><code>setRequestHeader(<var>header</var>,
790: <var>value</var>)</code></dfn> method is invoked, the user agent
1.217 avankest 791: <em class="ct">must</em> run these steps (unless otherwise indicated):</p>
1.14 avankest 792:
1.203 avankest 793: <ol>
794: <li><p>If the state is not <span title="OPENED state">OPENED</span> raise
795: an <code>INVALID_STATE_ERR</code> exception and terminate these
796: steps.</p></li>
797:
798: <li><p>If the <span><code>send()</code> flag</span> is true raise an
799: <code>INVALID_STATE_ERR</code> exception and terminate these
800: steps.</p></li>
801:
802: <li><p>If <var>header</var> does not match the <code>field-name</code>
803: production raise a <code>SYNTAX_ERR</code> exception and terminate these
804: steps.</p></li>
805:
806: <li>
807: <p>If the <var>value</var> argument does not match the
808: <code>field-value</code> production raise a <code>SYNTAX_ERR</code> and
809: terminate these steps.</p>
810:
811: <p class="note">The empty string is legal and represents the empty
812: header value.</p>
813: </li>
814:
815: <li>
816: <p>For security reasons, these steps <em class="ct">should</em> be
817: terminated if <var>header</var> is an
818: <span>ASCII case-insensitive</span> match for one of the following
819: headers:</p>
820:
821: <ul>
822: <li><code>Accept-Charset</code></li>
823: <li><code>Accept-Encoding</code></li>
824: <li><code>Connection</code></li>
825: <li><code>Content-Length</code></li>
826: <li><code>Cookie</code></li>
827: <li><code>Cookie2</code></li>
828: <li><code>Content-Transfer-Encoding</code></li>
829: <li><code>Date</code></li>
830: <li><code>Expect</code></li>
831: <li><code>Host</code></li>
832: <li><code>Keep-Alive</code></li>
833: <li><code>Referer</code></li>
834: <li><code>TE</code></li>
835: <li><code>Trailer</code></li>
836: <li><code>Transfer-Encoding</code></li>
837: <li><code>Upgrade</code></li>
838: <li><code>User-Agent</code></li>
839: <li><code>Via</code></li>
840: </ul>
1.96 avankest 841:
1.203 avankest 842: <p>… or if the start of <var>header</var> is an
843: <span>ASCII case-insensitive</span> match for <code>Proxy-</code> or
844: <code>Sec-</code> (including when <var>header</var> is just
845: <code>Proxy-</code> or <code>Sec-</code>).</p>
846:
847: <p class="note">The above headers are not allowed to be set as they are
848: better controlled by the user agent as it knows best what value they
849: should have. Header names starting with <code>Sec-</code> are not
850: allowed to be set to allow new headers to be minted in the future that
851: are guaranteed not to come from <code>XMLHttpRequest</code>. (Older
852: clients would however still be vulnerable as they allow such headers to
853: be set.)</p>
854: </li>
1.14 avankest 855:
1.203 avankest 856: <li><p>If <var>header</var> is not in the
857: <span>author request headers</span> list append <var>header</var> with
858: its associated <var>value</var> to the list and terminate these
859: steps.</p></li>
860:
861: <li><p>If <var>header</var> is in the <span>author request headers</span>
862: list either use multiple headers, combine the values or use a combination
863: of those (section 4.2, RFC 2616).
864: [<cite><span>RFC2616</span></cite>]</p></li>
865: <!-- XXX it seems UAs always combine the values -->
866: </ol>
1.45 avankest 867:
1.203 avankest 868: <p class="note">See also the <code>send()</code> method regarding user
869: agent header handling for caching, authentication, proxies, and
870: cookies.</p>
1.14 avankest 871:
1.203 avankest 872: <div class="example">
873: <pre><code>// The following script:
1.14 avankest 874: var client = new XMLHttpRequest();
875: client.open('GET', 'demo.cgi');
876: client.setRequestHeader('X-Test', 'one');
877: client.setRequestHeader('X-Test', 'two');
878: client.send();
879:
880: // ...would result in the following header being sent:
881: ...
882: X-Test: one, two
1.58 avankest 883: ...</code></pre>
1.203 avankest 884: </div>
1.248 avankest 885:
886:
1.218 avankest 887:
1.203 avankest 888:
889: <h4>The <code title="">send()</code> method</h4>
1.12 avankest 890:
1.235 avankest 891: <!-- XXX domintro
1.203 avankest 892: <p>The <code>send()</code> method initiates the request and its optional
1.204 avankest 893: argument provides the <span>request entity body</span>.</p>
1.235 avankest 894: -->
1.203 avankest 895:
896: <p>When the
897: <dfn id="send" title="send"><code>send(<var>data</var>)</code></dfn>
898: method is invoked, the user agent <em class="ct">must</em> run the
899: following steps (unless otherwise noted). This algorithm gets aborted when
900: the <code>open()</code> or <code>abort()</code> method is invoked. When
901: the
902: <dfn title="abort send()" id="abort-send-algorithm"><code>send()</code> algorithm is aborted</dfn>
903: the user agent <em class="ct">must</em> terminate the algorithm after
904: finishing the step it is on.</p>
905:
906: <p class="note">The <code title="">send()</code> algorithm can only be
907: aborted when the <span>asynchronous flag</span> is true and only after the
908: method call has returned.</p>
1.181 avankest 909:
1.203 avankest 910: <ol>
911: <li><p>If the state is not <span title="OPENED state">OPENED</span> raise
912: an <code>INVALID_STATE_ERR</code> exception and terminate these
913: steps.</p></li>
914:
915: <li><p>If the <span><code>send()</code> flag</span> is true raise an
916: <code>INVALID_STATE_ERR</code> exception and terminate these
917: steps.</p></li>
1.58 avankest 918:
1.203 avankest 919: <li>
920: <p>If the <span>request method</var> is <code>GET</code> or
1.239 avankest 921: <code>HEAD</code> act as if <var>data</var> is null.</p>
1.232 avankest 922:
923: <p>If the <var>data</var> argument has been omitted or is
1.239 avankest 924: null, do not include a <span>request entity body</span>
1.232 avankest 925: and go to the next step.</p>
1.248 avankest 926:
927: <p>Otherwise, let <var>encoding</var> be null, <var>mime type</var> be
928: null, and then follow these rules:</p>
1.203 avankest 929:
930: <dl class="switch">
1.248 avankest 931:
932:
933: <dt>If <var>data</var> is a <code>Document</code>
1.203 avankest 934:
935: <dd>
1.232 avankest 936: <p>Let <var>encoding</var> be the <span>preferred MIME name</span> of
937: the
938: <span title="document's character encoding">character encoding</span>
939: of <var>data</var>. If <var>encoding</var> is UTF-16 change it to
940: UTF-8.</p>
941:
1.248 avankest 942: <p>Let <var>mime type</var> be "<code>application/xml;charset=</code>"
943: followed by <var>encoding</var>.</p>
1.232 avankest 944:
945: <p>Let the <span>request entity body</span> be the result of getting
946: the <code title="dom-innerHTML">innerHTML</code> attribute on
1.233 avankest 947: <var>data</var>
948: <span title="convert a DOMString to a sequence of Unicode characters">converted to Unicode</span>
949: and encoded as <var>encoding</var>. Re-raise any exception this
950: raises.</p>
1.218 avankest 951:
1.233 avankest 952: <p class="note">In particular, if the document cannot be serialized an
1.217 avankest 953: <code>INVALID_STATE_ERR</code> exception is raised.</p>
954:
1.203 avankest 955: <p class="note">Subsequent changes to the <code>Document</code> have
956: no effect on what is submitted.</p>
957: </dd>
1.237 avankest 958:
1.248 avankest 959: <dt>If <var>data</var> is a <code>DOMString</code></dt>
1.237 avankest 960:
1.248 avankest 961: <dd>
962: <p>Let <var>encoding</var> be UTF-8.</p>
963:
964: <p>Let <var>mime type</var> be "<code>text/plain;charset=UTF-8</code>".</p>
965:
966: <p>Let the <span>request entity body</span> be <var>data</var>
967: <span title="convert a DOMString to a sequence of Unicode characters">converted to Unicode</span>
968: and encoded as UTF-8.</p>
969: </dd>
970:
971:
1.203 avankest 972: </dl>
1.58 avankest 973:
1.232 avankest 974: <p>If a <code>Content-Type</code> header is set using
1.248 avankest 975: <code>setRequestHeader()</code> whose value is a
976: <span>valid MIME type</span> and has a <code>charset</code>
1.232 avankest 977: parameter whose value is not an <span>ASCII case-insensitive</span>
1.248 avankest 978: match for <var>encoding</var>, and <var>encoding</var> is not null, set
979: all the <code>charset</code> parameters of the
980: <code>Content-Type</code> header to <var>encoding</var>.</p>
1.232 avankest 981:
982: <p>If no <code>Content-Type</code> header has been set using
1.248 avankest 983: <code>setRequestHeader()</code> and <var>mime type</var> is not null set
984: a <code>Content-Type</code> request header with as value
985: <var>mime type</var>.</p>
1.236 avankest 986:
987: <!-- reminder: if we ever change this to always include charset it has
988: to be included as the first parameter for compatibility reasons -->
1.203 avankest 989: </li>
1.202 avankest 990:
1.203 avankest 991: <li><p>If the <span>asynchronous flag</span> is false release the
992: <span>storage mutex</span>.</p></li>
1.103 avankest 993:
1.248 avankest 994:
995:
1.203 avankest 996: <li><p>Set the <span>error flag</span> to false.</p></li>
1.218 avankest 997:
1.248 avankest 998:
999:
1.211 avankest 1000: <li>
1001: <p>If the <span>asynchronous flag</span> is true run these substeps:</p>
1.218 avankest 1002:
1.211 avankest 1003: <ol>
1.217 avankest 1004: <li><p>Set the <span><code>send()</code> flag</span> to true.</p></li>
1005:
1.211 avankest 1006: <li>
1007: <p><span>Dispatch a <code>readystatechange</code> event</span>.</p>
1.145 avankest 1008:
1.211 avankest 1009: <p class="note">The state does not change. The event is dispatched for
1010: historical reasons.</p>
1011: </li>
1012:
1.248 avankest 1013:
1014:
1.211 avankest 1015: <li><p>Return the <code>send()</code> method call, but continue running
1016: the steps in this algorithm.</p></li>
1017: </ol>
1018: </li>
1.218 avankest 1019:
1.203 avankest 1020: <li>
1.248 avankest 1021:
1.211 avankest 1022:
1.248 avankest 1023: <p>Make a request to <span>request URL</span>, using HTTP method
1024: <span>request method</span>, user <span>request username</span> (if
1025: non-null) and password <span>request password</span> (if non-null),
1026: taking into account the <span>request entity body</span>, list of
1027: <span>author request headers</span> and the rules listed at the end of
1028: this section.</p>
1029:
1030: <p>If there are cookies to be set, run the
1031: <span>cookie steps</span>.</p>
1032:
1033: <dl class="switch">
1034: <dt>If the <span>asynchronous flag</span> is false</dt>
1035: <dd>
1036: <p>While making the request also follow the
1037: <span>same-origin request event rules</span>.</p>
1038:
1039: <!--
1040: This cannot involve any task queue whatsoever because that would
1041: mean other tasks on the task queue might get processed as well
1042: which is counter to the whole idea of doing things synchronous.
1043: -->
1044:
1045: <p class="note">The <code>send()</code> method call will now be
1046: returned by virtue of this algorithm ending.</p>
1047: </dd>
1048:
1049: <dt>If the <span>asynchronous flag</span> is true</dt>
1050: <dd>
1051: <p><span>Make progress notifications</span>.</p>
1052:
1053: <p><span>Make upload progress notifications</span>.</p>
1054:
1055: <p>While processing the request, as data becomes available and when
1056: the user interferes with the request,
1057: <span title="queue a task">queue tasks</span> to follow the
1058: <span>same-origin request event rules</span> using the
1059: <span><code>XMLHttpRequest</code> networking task source</span> as
1060: <span>task source</span>.</p>
1061: </dd>
1062: </dl>
1063:
1.203 avankest 1064: </li>
1065: </ol>
1.60 avankest 1066:
1.203 avankest 1067: <hr>
1068:
1.227 avankest 1069: <p>If the user agent allows the end user to configure a proxy it
1.248 avankest 1070: <em class="ct">should</em> modify the request appropriately; i.e., connect
1071: to the proxy host instead of the origin server, modify the
1072: <code>Request-Line</code> and send <code>Proxy-Authorization</code>
1073: headers as specified.</p>
1.212 avankest 1074:
1.218 avankest 1075: <p>If the user agent supports HTTP Authentication and
1076: <code>Authorization</code> is not in the list of
1077: <span>author request headers</span>, it <em class="ct">should</em>
1078: consider requests originating from the <code>XMLHttpRequest</code> object
1079: to be part of the protection space that includes the accessed URIs and
1080: send <code>Authorization</code> headers and handle
1081: <code>401 Unauthorized</code> requests appropriately. If authentication
1.247 avankest 1082: fails, and <span>request username</span> and <span>request password</span>
1.227 avankest 1083: are both null, user agents <em class="ct">should</em> prompt the end user
1084: for credentials. If authentication fails, and
1085: <span>request username</span> and <span>request password</span> are
1086: provided, user agents <em class="ct">must not</em> prompt the end user for
1087: credentials. [<cite><span>RFC2617</span></cite>]</p>
1.212 avankest 1088:
1.227 avankest 1089: <p class="note">End users are not prompted if credentials are provided
1.212 avankest 1090: through the <code>open()</code> API so that authors can implement their
1091: own user interface.</p>
1092:
1093: <p>If the user agent supports HTTP State Management it
1094: <em class="ct">should</em> persist, discard and send cookies (as received
1095: in the <code>Set-Cookie</code> and <code>Set-Cookie2</code> response
1096: headers, and sent in the <code>Cookie</code> header) as applicable.
1097: [<cite><span>COOKIES</span></cite>]</p>
1098:
1099: <p>If the user agent implements a HTTP cache it <em class="ct">should</em>
1.248 avankest 1100: respect <code>Cache-Control</code> request headers set by the
1101: <code>setRequestHeader()</code> (e.g., <code>Cache-Control:
1102: no-cache</code> bypasses the cache). It <em class="ct">must not</em> send
1.212 avankest 1103: <code>Cache-Control</code> or <code>Pragma</code> request headers
1.227 avankest 1104: automatically unless the end user explicitly requests such behavior
1.212 avankest 1105: (e.g. by (force-)reloading the page).</p>
1106:
1107: <p>For <code>304 Not Modified</code> responses that are a result of a
1108: user agent generated conditional request the user agent
1109: <em class="ct">must</em> act as if the server gave a <code>200 OK</code>
1110: response with the appropriate content. The user agent
1111: <em class="ct">must</em> allow <code>setRequestHeader()</code> to
1.248 avankest 1112: override automatic cache validation by setting request headers (e.g.
1113: <code>If-None-Match</code> or <code>If-Modified-Since</code>), in which
1.212 avankest 1114: case <code>304 Not Modified</code> responses <em class="ct">must</em> be
1115: passed through. [<cite><span>RFC2616</span></cite>]</p>
1116:
1117: <p>If the user agent implements server-driven content-negotiation
1118: it <em class="ct">should</em> set <code>Accept-Encoding</code> and
1.231 avankest 1119: <code>Accept-Charset</code> headers as appropriate. For
1120: <code>Accept</code> and <code>Accept-Language</code> the user agent
1121: <em class="ct">must</em> follow these constraints:</p>
1122:
1123: <ul>
1124: <li><p>Both headers <em class="ct">must not</em> be modified if they are
1125: already set through <code>setRequestHeader()</code>.</p></li>
1126:
1127: <li><p>If not set through <code>setRequestHeader()</code>
1128: <code>Accept-Language</code> <em class="ct">should</em> set as
1129: appropriate.</p></li>
1130:
1131: <li><p>If not set through <code>setRequestHeader()</code>
1132: <code>Accept</code> <em class="ct">must</em> be set with as value
1133: <code>*/*</code>.</p></li>
1134: </ul>
1135:
1136: <p>Responses <em class="ct">must</em> have the content-encodings
1137: automatically decoded. [<cite><span>RFC2616</span></cite>]</p>
1.212 avankest 1138:
1139: <p>Besides the <span>author request headers</span> user agents
1140: <em class="ct">should not</em> include additional request headers other
1.248 avankest 1141: than those mentioned above or other than those authors are not allowed to
1142: set using <code>setRequestHeader()</code>. This ensures that authors have
1143: a reasonably predictable API.</p>
1.218 avankest 1144:
1.212 avankest 1145:
1146: <h4>Infrastructure for the <code title="">send()</code> method</h4>
1.216 avankest 1147:
1148: <p>The <dfn>cookie steps</dfn> are as follows:</p>
1.218 avankest 1149:
1.216 avankest 1150: <ol>
1151: <li><p>Wait until ownership of the <span>storage mutex</span> can be
1152: taken.</p></li>
1.218 avankest 1153:
1.216 avankest 1154: <li><p>Take ownership of the <span>storage mutex</span>.</p></li>
1.218 avankest 1155:
1.216 avankest 1156: <li><p>Update the cookies. [<cite><span>COOKIES</span></cite>]</p></li>
1157:
1158: <li><p>Release the <span>storage mutex</span> so that it is once again
1159: free.</p></li>
1160: </ol>
1.218 avankest 1161:
1.216 avankest 1162: <hr>
1.218 avankest 1163:
1.219 avankest 1164: <p>The <dfn>same-origin request event rules</dfn> are as follows:</p>
1.118 avankest 1165:
1.203 avankest 1166: <dl class="switch">
1167: <dt>If the response is an HTTP redirect</dt>
1168: <dd>
1.248 avankest 1169:
1.203 avankest 1170: <p>If the redirect does not violate security (it is
1171: <span>same origin</span> for instance), infinite loop precautions, and
1172: the scheme is supported, transparently follow the redirect while
1.219 avankest 1173: observing the <span>same-origin request event rules</span>.</p>
1.248 avankest 1174:
1175: <p>Otherwise, this is a <span>network error</span>.</p>
1176:
1177:
1.203 avankest 1178:
1179: <p class="note">HTTP places requirements on the user agent regarding the
1.204 avankest 1180: preservation of the <span>request method</span> and
1.248 avankest 1181: <span>request entity body</span> during redirects, and also requires end
1182: users to be notified of certain kinds of automatic redirections.</p>
1.203 avankest 1183: <!-- XXX HTTP needs fixing here -->
1.213 avankest 1184: </dd>
1.121 avankest 1185:
1.227 avankest 1186: <dt>If the end user cancels the download</dt>
1.213 avankest 1187: <dd><p>This is an <span>abort error</span>.</p></dd>
1.60 avankest 1188:
1.214 avankest 1189: <dt>In case of network errors</dt>
1.213 avankest 1190: <dd>
1191: <p>In case of DNS errors, TLS negotiation failure, or other type of
1192: network errors, this is a <span>network error</span>. Do not request any
1.227 avankest 1193: kind of end user interaction.</p>
1.121 avankest 1194:
1.213 avankest 1195: <p class="note">This does not include HTTP responses that indicate
1196: some type of error, such as HTTP status code 410.</p>
1197: </dd>
1.60 avankest 1198:
1.248 avankest 1199:
1200:
1.213 avankest 1201: <dt>Once all HTTP headers have been received and the
1202: <span>asynchronous flag</span> is true</dt>
1203: <dd><p><span>Switch to the HEADERS_RECEIVED state</span>.</p></dd>
1.186 avankest 1204:
1.220 avankest 1205: <dt>Once the first byte (or more) of the
1206: <span>response entity body</span> has been received and the
1207: <span>asynchronous flag</span> is true</dt>
1208: <dt>If there is no <span>response entity body</span> and the
1.213 avankest 1209: <span>asynchronous flag</span> is true</dt>
1.215 avankest 1210: <dd><p><span>Switch to the LOADING state</span>.</p></dd>
1.220 avankest 1211:
1212: <dt>Once the whole <span>response entity body</span> has been
1213: received</dt>
1.224 avankest 1214: <dt>If there is no <span>response entity body</span> and the
1215: <span>asynchronous flag</span> is false or the state is
1.221 avankest 1216: <span title="LOADING state">LOADING</span></dt>
1.220 avankest 1217: <dd><p><span>Switch to the DONE state</span>.</p></dd>
1.213 avankest 1218: </dl>
1.218 avankest 1219:
1.248 avankest 1220:
1221:
1.213 avankest 1222: <hr>
1.218 avankest 1223:
1.241 avankest 1224: <p>When something is said to be a <dfn>network error</dfn> run the
1225: <span>request error</span> steps for exception
1226: <code>NETWORK_ERR</code>.</p>
1227:
1228: <p>When something is said to be an <dfn>abort error</dfn> run the
1229: <span>request error</span> steps for exception
1230: <code>ABORT_ERR</code>.</p>
1.60 avankest 1231:
1.248 avankest 1232:
1233:
1.242 avankest 1234: <p>When something is said to be a <dfn>request error</dfn> for
1.248 avankest 1235: exception <var>exception</var> run these
1236: steps:</p>
1.12 avankest 1237:
1.213 avankest 1238: <ol>
1239: <li><p>Set the <span>response entity body</span> to null.</p></li>
1.203 avankest 1240:
1.213 avankest 1241: <li><p>Set the the <span>error flag</span> to true.</p></li>
1.12 avankest 1242:
1.213 avankest 1243: <li><p>Empty the list of <span>author request headers</span>.</p></li>
1.203 avankest 1244:
1.213 avankest 1245: <li><p>Switch the state to <span title="DONE state">DONE</span>.</p></li>
1.203 avankest 1246:
1.241 avankest 1247: <li><p>If the <span>asynchronous flag</span> is false raise an
1248: <var>exception</var> exception and terminate the overall set of
1.213 avankest 1249: steps.</p></li>
1.203 avankest 1250:
1.248 avankest 1251: <li>
1.249 ! avankest 1252: <p><span>Dispatch a <code>readystatechange</code> event</span>.</p>
1.248 avankest 1253:
1254: <p class="note">At this point it is clear that the
1255: <span>asynchronous flag</span> is true.</p>
1256: </li>
1257:
1258:
1.203 avankest 1259:
1.248 avankest 1260: <li><p>Terminate the overall algorithm.</p></li>
1.213 avankest 1261: </ol>
1.203 avankest 1262:
1.248 avankest 1263:
1264: <p class="note">A future version of this specification will dispatch an
1265: <code>error</code>/<code>abort</code> event here as well. (Depending on
1266: the type of error.)</p>
1267:
1.203 avankest 1268:
1.213 avankest 1269: <hr>
1.203 avankest 1270:
1.213 avankest 1271: <p>When it is said to
1272: <dfn id="switch-headers-received">switch to the HEADERS_RECEIVED state</dfn>
1273: run these steps:</p>
1.203 avankest 1274:
1.213 avankest 1275: <ol>
1276: <li><p>Switch the state to <span title="HEADERS_RECEIVED state">HEADERS_RECEIVED</span>.</p></li>
1.203 avankest 1277:
1.249 ! avankest 1278: <li><p><span>Dispatch a <code>readystatechange</code> event</span>.</p></li>
1.213 avankest 1279: </ol>
1.218 avankest 1280:
1.213 avankest 1281: <hr>
1.60 avankest 1282:
1.213 avankest 1283: <p>When it is said to
1284: <dfn id="switch-loading">switch to the LOADING state</dfn> run these
1285: steps:</p>
1.218 avankest 1286:
1.213 avankest 1287: <ol>
1288: <li><p>Switch the state to <span title="LOADING state">LOADING</span>.</p></li>
1.203 avankest 1289:
1.249 ! avankest 1290: <li><p><span>Dispatch a <code>readystatechange</code> event</span>.</p></li>
1.213 avankest 1291: </ol>
1.218 avankest 1292:
1.216 avankest 1293: <hr>
1294:
1295: <p>When it is said to
1296: <dfn id="switch-done">switch to the DONE state</dfn> run these steps:</p>
1.218 avankest 1297:
1.216 avankest 1298: <ol>
1299: <li><p>Switch the state to <span title="DONE state">DONE</span>.</p></li>
1300:
1.248 avankest 1301: <li><p><span>Dispatch a <code>readystatechange</code> event</span>.</p></li>
1302:
1.220 avankest 1303:
1.216 avankest 1304: </ol>
1.218 avankest 1305:
1.248 avankest 1306:
1307:
1.203 avankest 1308:
1309: <h4>The <code title="">abort()</code> method</h4>
1310:
1311: <p>When the <dfn id="abort"><code>abort()</code></dfn> method is invoked,
1.217 avankest 1312: the user agent <em class="ct">must</em> run these steps (unless otherwise
1313: noted):</p>
1.12 avankest 1314:
1.203 avankest 1315: <ol>
1316: <li><p><span title="abort send()">Abort the <code>send()</code> algorithm</span>.</p></li>
1317:
1318: <li><p>The user agent <em class="ct">should</em> cancel any network
1319: activity for which the object is responsible.</p></li>
1320:
1321: <li><p>Set the <span>response entity body</span> to null.</p></li>
1322:
1323: <li><p>Set the <span>error flag</span> to true.</p></li>
1324:
1325: <li><p>Empty the list of <span>author request headers</span>.</p></li>
1326:
1327: <li>
1328: <p>If the state is <span title="UNSENT state">UNSENT</span>,
1329: <span title="OPENED state">OPENED</span> with the
1330: <span><code>send()</code> flag</span> being false, or
1331: <span title="DONE state">DONE</span> go to the next step.</p>
1.12 avankest 1332:
1.203 avankest 1333: <p>Otherwise run these substeps:</p>
1.58 avankest 1334:
1335: <ol>
1.203 avankest 1336: <li><p>Switch the state to <span title="DONE state">DONE</span>.</p></li>
1.170 avankest 1337:
1.203 avankest 1338: <li><p>Set the <span><code>send()</code> flag</span> to false.</p></li>
1.96 avankest 1339:
1.203 avankest 1340: <li><p><span>Dispatch a <code>readystatechange</code> event</span>.</p></li>
1.248 avankest 1341:
1342:
1.58 avankest 1343: </ol>
1.248 avankest 1344:
1345:
1.218 avankest 1346: <p class="note">It is likely that a future version of this specification
1347: will dispatch an <code title="">abort</code> event here.</p>
1.248 avankest 1348:
1.203 avankest 1349: </li>
1350:
1351: <li>
1352: <p>Switch the state to <span title="UNSENT state">UNSENT</span>.</p>
1353:
1354: <p class="note">No <code>readystatechange</code> event is dispatched.</p>
1355: </li>
1356: </ol>
1357:
1358:
1359:
1.205 avankest 1360: <h3 id="response">Response</h3>
1.203 avankest 1361:
1362:
1363: <h4>The <code title="">status</code> attribute</h4>
1364:
1365: <p>The <dfn id="status"><code>status</code></dfn> attribute
1.222 avankest 1366: <em class="ct">must</em> return the result of running these steps:</p>
1.226 avankest 1367:
1.222 avankest 1368: <ol>
1369: <li><p>If the state is <span title="UNSENT state">UNSENT</span> or
1.239 avankest 1370: <span title="OPENED state">OPENED</span> return 0 and terminate these
1.222 avankest 1371: steps.</p></li>
1372:
1.239 avankest 1373: <li><p>If the <span>error flag</span> is true return 0 and terminate
1374: these steps.</p></li>
1.222 avankest 1375:
1376: <li><p>Return the HTTP status code.</p></li>
1.248 avankest 1377: </ol>
1.203 avankest 1378:
1379:
1380: <h4>The <code title="">statusText</code> attribute</h4>
1381:
1382: <p>The <dfn id="statustext"><code>statusText</code></dfn> attribute
1.222 avankest 1383: <em class="ct">must</em> return the result of running these steps:</p>
1384:
1385: <ol>
1386: <li><p>If the state is <span title="UNSENT state">UNSENT</span> or
1.239 avankest 1387: <span title="OPENED state">OPENED</span> return the empty string and
1388: terminate these steps.</p></li>
1.222 avankest 1389:
1390: <li><p>If the <span>error flag</span> is true return the empty string and
1391: terminate these steps.</p></li>
1392:
1393: <li><p>Return the HTTP status text.</p></li>
1394: </ol>
1.203 avankest 1395:
1396:
1397: <h4>The <code title="">getResponseHeader()</code> method</h4>
1398:
1399: <p>When the
1400: <dfn id="getresponseheader" title="getresponseheader"><code>getResponseHeader(<var>header</var>)</code></dfn>
1.217 avankest 1401: is invoked, the user agent <em class="ct">must</em> run these steps:</p>
1.203 avankest 1402:
1403: <ol>
1404: <li><p>If the state is <span title="UNSENT state">UNSENT</span> or
1.239 avankest 1405: <span title="OPENED state">OPENED</span> return null and terminate these
1.203 avankest 1406: steps.</p></li>
1407:
1.239 avankest 1408: <li><p>If the <span>error flag</span> is true return null
1.203 avankest 1409: and terminate these steps.</p></li>
1410:
1411: <li><p>If <var>header</var> is an <span>ASCII case-insensitive</span>
1412: match for <code>Set-Cookie</code> or <code>Set-Cookie2</code> return
1.239 avankest 1413: null and terminate these steps.</p></li>
1.203 avankest 1414:
1415: <li><p>If <var>header</var> is an <span>ASCII case-insensitive</span>
1416: match for multiple HTTP response headers, return the values of these
1417: headers as a single concatenated string separated from each other by a
1418: U+002C COMMA U+0020 SPACE character pair and terminate these
1419: steps.</p></li>
1420:
1421: <li><p>If <var>header</var> is an <span>ASCII case-insensitive</span>
1422: match for a single HTTP response header, return the value of that header
1423: and terminate these steps.</p></li>
1424:
1.239 avankest 1425: <li><p>Return null.</p></li>
1.203 avankest 1426: </ol>
1427:
1.248 avankest 1428:
1429:
1.203 avankest 1430: <div class="example">
1431: <pre><code>// The following script:
1.5 avankest 1432: var client = new XMLHttpRequest();
1.14 avankest 1433: client.open("GET", "test.txt", true);
1.5 avankest 1434: client.send();
1.12 avankest 1435: client.onreadystatechange = function() {
1.179 avankest 1436: if(this.readyState == 2) {
1.203 avankest 1437: print(client.getResponseHeader("Content-Type"));
1.12 avankest 1438: }
1439: }
1.5 avankest 1440:
1441: // ...should output something similar to the following text:
1.58 avankest 1442: Content-Type: text/plain; charset=utf-8</code></pre>
1.203 avankest 1443: </div>
1.12 avankest 1444:
1445:
1.203 avankest 1446: <h4>The <code title="">getAllResponseHeaders()</code> method</h4>
1.14 avankest 1447:
1.203 avankest 1448: <p>When the
1449: <dfn id="getallresponseheaders"><code>getAllResponseHeaders()</code></dfn>
1450: method is invoked, the user agent <em class="ct">must</em> run the
1451: following steps:</p>
1.58 avankest 1452:
1.203 avankest 1453: <ol>
1454: <li><p>If the state is <span title="UNSENT state">UNSENT</span> or
1.239 avankest 1455: <span title="OPENED state">OPENED</span> return the empty string and
1456: terminate these steps.</p></li>
1.203 avankest 1457:
1458: <li><p>If the <span>error flag</span> is true return the empty string and
1459: terminate these steps.</p></li>
1460:
1461: <li><p>Return all the HTTP headers, excluding headers that are an
1462: <span>ASCII case-insensitive</span> match for <code>Set-Cookie</code> or
1463: <code>Set-Cookie2</code>, as a single string, with each header line
1464: separated by a U+000D CR U+000A LF pair excluding the status line, and
1465: with each header name and header value separated by a
1466: U+003A COLON U+0020 SPACE pair.</p></li>
1467: </ol>
1.58 avankest 1468:
1.203 avankest 1469: <div class="example">
1470: <pre><code>// The following script:
1.1 avankest 1471: var client = new XMLHttpRequest();
1.14 avankest 1472: client.open("GET", "test.txt", true);
1.5 avankest 1473: client.send();
1.12 avankest 1474: client.onreadystatechange = function() {
1.179 avankest 1475: if(this.readyState == 2) {
1.203 avankest 1476: print(this.getAllResponseHeaders());
1.12 avankest 1477: }
1478: }
1.1 avankest 1479:
1.5 avankest 1480: // ...should output something similar to the following text:
1.203 avankest 1481: Date: 2004年10月24日 04:58:38 GMT
1482: Server: Apache/1.3.31 (Unix)
1483: Keep-Alive: timeout=15, max=99
1484: Connection: Keep-Alive
1485: Transfer-Encoding: chunked
1486: Content-Type: text/plain; charset=utf-8</code></pre>
1487: </div>
1488:
1.248 avankest 1489:
1.203 avankest 1490:
1491: <h4>Response Entity Body</h4>
1492:
1.248 avankest 1493: <!-- XXX carefully study this -->
1494:
1495: <p>The <dfn id="response-mime-type">response MIME type</dfn> is the MIME
1496: type the <code>Content-Type</code> header contains without any
1497: parameters or null if the header could not be parsed properly or was
1498: omitted. The <dfn id="override-mime-type">override MIME type</dfn> is
1499: always null. <dfn id="final-mime-type">Final MIME type</dfn> is the
1500: override MIME type unless that is null in which case it is the response
1501: MIME type.</p>
1502:
1503: <p>The <dfn id="response-charset">response charset</dfn> is the value of
1504: the <code>charset</code> parameter of the <code>Content-Type</code> header
1505: or null if there was no <code>charset</code> parameter or if
1506: the header could not be parsed properly or was omitted. The
1507: <dfn id="override-charset">override charset</dfn> is always null.
1508: <dfn id="final-charset">Final charset</dfn> is the override charset unless
1509: that is null in which case it is the response charset.</p>
1510:
1511:
1512: <p class="note"><span>Override MIME type</span> and
1513: <span>override charset</span> are introduced here solely to make editing
1514: several levels of XMLHttpRequest simultaneously somewhat easier. Apologies
1515: for any confusion they might cause.</p>
1516:
1517:
1518: <hr>
1519:
1.203 avankest 1520: <p>The <dfn id="response-entity-body">response entity body</dfn> is the
1.248 avankest 1521: fragment of the <span>entity body</span> received so far (LOADING state) or
1522: the complete entity body (DONE state). If there is no entity body the
1523: response entity body is null.</p>
1524:
1.203 avankest 1525: <hr>
1526:
1527: <p>The <dfn id="text-response-entity-body">text response entity body</dfn>
1.248 avankest 1528: is a <code>DOMString</code> representing the <span>response entity
1529: body</span>. The text response entity body is the return value of the
1.203 avankest 1530: following algorithm:</p>
1531:
1532: <ol>
1.248 avankest 1533: <li><p>If the response entity body is null return the empty string and
1534: terminate these steps.</p>
1535:
1536: <li><p>Let <var>charset</var> be the <span>final charset</span>.</p></li>
1.203 avankest 1537:
1.248 avankest 1538: <li><p>Let <var>mime</var> be the <span>final MIME type</span>.</p></li>
1.203 avankest 1539:
1.248 avankest 1540: <li><p>If <var>charset</var> is null and <var>mime</var> is null,
1.203 avankest 1541: <code>text/xml</code>, <code>application/xml</code> or ends in
1.248 avankest 1542: <code title="">+xml</code> use the rules set forth in the XML
1543: specifications to determine the character encoding. Let
1544: <var>charset</var> be the determined character encoding.</p></li>
1545:
1546: <li><p>If <var>charset</var> is null and <var>mime</var> is
1547: <code>text/html</code> follow the rules set forth in the HTML
1548: specification to determine the character encoding. Let
1.203 avankest 1549: <var>charset</var> be the determined character encoding.
1550: [<cite><span>HTML5</span></cite>]</p></li>
1551:
1552: <li>
1553: <p>If <var>charset</var> is null then, for each of the rows in the
1554: following table, starting with the first one and going down, if the first
1555: bytes of <var>bytes</var> match the bytes given in the first column, then
1556: let <var>charset</var> be the encoding given in the cell in the second
1557: column of that row. If there is no match <var>charset</var> remains
1558: null.</p>
1559:
1560: <table>
1561: <thead>
1562: <tr>
1563: <th>Bytes in Hexadecimal
1564: <th>Description
1.237 avankest 1565: <tbody>
1.203 avankest 1566: <tr>
1567: <td>FE FF
1568: <td>UTF-16BE BOM
1569: <tr>
1570: <td>FF FE
1571: <td>UTF-16LE BOM
1572: <tr>
1573: <td>EF BB BF
1.237 avankest 1574: <td>UTF-8 BOM
1.203 avankest 1575: </table>
1576: </li>
1577:
1578: <li><p>If <var>charset</var> is null let <var>charset</var> be
1579: UTF-8.</p></li>
1580:
1581: <li><p>Return the result of decoding the response entity body using
1582: <var>charset</var>. Replace bytes or sequences of bytes that are not
1.248 avankest 1583: valid accordng to the <var>charset</var> with a single
1584: U+FFFD REPLACEMENT CHARACTER character.</p></li>
1.203 avankest 1585: </ol>
1586:
1587: <p class="note">Authors are strongly encouraged to encode their resources
1588: using UTF-8.</p>
1.248 avankest 1589:
1.203 avankest 1590: <hr>
1591:
1.248 avankest 1592: <p>The <dfn id="document-response-entity-body">document response entity
1593: body</dfn> is either a <code>Document</code> representing the
1594: <span>response entity body</span> or null. The document
1595: response entity body is the return value of the following algorithm:</p>
1.12 avankest 1596:
1.203 avankest 1597: <ol>
1598: <li><p>If the <span>response entity body</span> is null terminate these
1.239 avankest 1599: steps and return null.</p></li>
1.12 avankest 1600:
1.248 avankest 1601: <li><p>If <span>final MIME type</span> is not null,
1602: <code>text/xml</code>,
1603: <code>application/xml</code>, and does not end in
1604: <code title="">+xml</code> terminate these steps and return
1605: null.</li>
1606:
1607:
1.203 avankest 1608:
1609: <li>
1.248 avankest 1610: <p>Let <var>document</var> be an object implementing the
1611: <code>Document</code> interface that represents the result of parsing
1612: the response entity body into a document tree following the rules from
1613: the XML specifications. If this fails (unsupported character encoding,
1614: namespace well-formedness error et cetera) terminate these steps return
1615: null. [<cite><span>XML</span></cite>]</p>
1616:
1617: <p class="note">Scripts in the resulting document tree will not be executed,
1618: resources referenced will not be loaded and no associated XSLT will be
1619: applied.</p>
1.203 avankest 1620: </li>
1.88 avankest 1621:
1.248 avankest 1622:
1623:
1624: <li><p>Return <var>document</var>.</p></li>
1.203 avankest 1625: </ol>
1.12 avankest 1626:
1627:
1.248 avankest 1628:
1629:
1630:
1.203 avankest 1631: <h4>The <code title="">responseText</code> attribute</h4>
1.12 avankest 1632:
1.203 avankest 1633: <p>The <dfn id="responsetext"><code>responseText</code></dfn> attribute
1.217 avankest 1634: <em class="ct">must</em> return the result of running these steps:</p>
1.79 avankest 1635:
1.203 avankest 1636: <ol>
1.248 avankest 1637: <li><p>If the state is not <span title="LOADING state">LOADING</span> or
1638: <span title="DONE state">DONE</span> return the empty string and
1.203 avankest 1639: terminate these steps.</p></li>
1.75 avankest 1640:
1.203 avankest 1641: <li><p>Return the <span>text response entity body</span>.</p></li>
1642: </ol>
1.12 avankest 1643:
1644:
1.203 avankest 1645: <h4>The <code title="">responseXML</code> attribute</h4>
1.12 avankest 1646:
1.203 avankest 1647: <p>The <dfn id="responsexml"><code>responseXML</code></dfn> attribute
1.217 avankest 1648: <em class="ct">must</em> return the result of running these steps:</p>
1.12 avankest 1649:
1.203 avankest 1650: <ol>
1651: <li><p>If the state is not <span title="DONE state">DONE</span> return
1.239 avankest 1652: null and terminate these steps.</p></li>
1.12 avankest 1653:
1.248 avankest 1654: <li><p>Return the <span>document response entity body</span>.</p></li>
1.203 avankest 1655: </ol>
1.121 avankest 1656:
1.248 avankest 1657:
1658:
1.12 avankest 1659:
1.240 avankest 1660: <h2 id="exceptions">Exceptions</h2>
1.24 avankest 1661:
1.138 avankest 1662: <p>Several algorithms in this specification may result in an exception
1663: being thrown. These exceptions are all part of the group
1.185 avankest 1664: <code>ExceptionCode</code> and use the <code>DOMException</code> object,
1.138 avankest 1665: which is defined in DOM Level 3 Core. In addition this specification
1666: extends the <code>ExceptionCode</code> group with several new
1667: constants as indicated below. [<cite><span>DOM3Core</span></cite>]</p>
1.203 avankest 1668:
1.193 avankest 1669: <p class="note">Thus, exceptions used by this specification and not
1670: defined in this section are defined by DOM Level 3 Core.</p>
1.31 avankest 1671:
1.138 avankest 1672: <pre class="idl">const unsigned short <span>SECURITY_ERR</span> = 18;
1.199 avankest 1673: const unsigned short <span>NETWORK_ERR</span> = 19;
1674: const unsigned short <span>ABORT_ERR</span> = 20;</pre>
1.31 avankest 1675:
1.138 avankest 1676: <p>The <dfn id="security-err"><code>SECURITY_ERR</code></dfn> exception is
1677: raised if an attempt is made to perform an operation or access some data
1678: in a way that would be a security risk or a violation of the user agent's
1679: security policy.</p>
1680:
1681: <!-- http://lists.w3.org/Archives/Public/public-webapi/2006May/0027.html -->
1682:
1.33 avankest 1683: <p>The <dfn id="network-err"><code>NETWORK_ERR</code></dfn> exception is
1.138 avankest 1684: raised when a network error occurs in synchronous requests.</p>
1.121 avankest 1685:
1.138 avankest 1686: <p>The <dfn id="abort-err"><code>ABORT_ERR</code></dfn> exception is
1687: raised when the user aborts a request in synchronous requests.</p>
1.248 avankest 1688:
1689:
1.203 avankest 1690:
1.240 avankest 1691: <p class="note">These exceptions will be folded into an update of
1.199 avankest 1692: DOM Level 3 Core in due course, as they are appropriate for other API
1693: specifications as well.</p>
1.121 avankest 1694:
1.248 avankest 1695:
1.29 avankest 1696: <h2 class="no-num" id="notcovered">Not in this Specification</h2>
1697:
1.143 avankest 1698: <p><em>This section is non-normative.</em></p>
1.29 avankest 1699:
1.72 avankest 1700: <p>This specification does not include the following features which are
1701: being considered for a future version of this specification:</p>
1.29 avankest 1702:
1703: <ul>
1704: <li><code>load</code> event and <code>onload</code> attribute;</li>
1705: <li><code>error</code> event and <code>onerror</code> attribute;</li>
1706: <li><code>progress</code> event and <code>onprogress</code> attribute;</li>
1707: <li><code title="">abort</code> event and <code>onabort</code> attribute;</li>
1708: <li>Timers have been suggested, perhaps an <code>ontimeout</code>
1709: attribute;</li>
1710: <li>Property to disable following redirects;</li>
1.30 avankest 1711: <li><code title="">responseXML</code> for <code>text/html</code>
1712: documents;</li>
1.203 avankest 1713: <li>Cross-origin <code title="">XMLHttpRequest</code>;</li>
1.87 avankest 1714: <li><code>responseBody</code> to deal with byte streams;</li>
1.114 avankest 1715: <li><code>overrideMimeType</code> to fix up MIME types;</li>
1.87 avankest 1716: <li><code>getRequestHeader()</code> and
1717: <code>removeRequestHeader()</code>.</li>
1.29 avankest 1718: </ul>
1.248 avankest 1719:
1.29 avankest 1720:
1.203 avankest 1721:
1.248 avankest 1722:
1723:
1.203 avankest 1724:
1.248 avankest 1725: <h2 class="no-num" id="references">References</h2>
1.203 avankest 1726:
1.177 avankest 1727: <p>Unless marked "Non-normative" these references are normative.</p>
1.11 avankest 1728:
1.6 avankest 1729: <dl>
1.230 avankest 1730: <dt>[<dfn id="ref-cookies">COOKIES</dfn>]</dt>
1.203 avankest 1731: <dd><cite><a href="http://ietf.org/rfc/rfc2109">HTTP State Management
1732: Mechanism</a></cite>, D. Kristol, L. Montulli, editors. IETF, February
1733: 1997.</dd>
1734: <dd><cite><a href="http://ietf.org/rfc/rfc2965">HTTP State Management
1735: Mechanism</a></cite>, D. Kristol, L. Montulli, editors. IETF, October
1.218 avankest 1736: 2000.</dd>
1.211 avankest 1737:
1.203 avankest 1738: <!-- XXX These specs do not match reality. Also, the latter obsoletes the
1739: former -->
1740:
1.248 avankest 1741:
1742:
1.155 avankest 1743: <dt>[<dfn id="ref-dom2events">DOM2Events</dfn>]</dt>
1744: <dd><cite><a href="http://www.w3.org/TR/DOM-Level-2-Events/">Document
1.160 avankest 1745: Object Model (DOM) Level 2 Events Specification</a></cite>, T. Pixley,
1746: editor. W3C, November 2000.</dd>
1.155 avankest 1747:
1.145 avankest 1748: <dt>[<dfn id="ref-dom3core">DOM3Core</dfn>]</dt>
1.11 avankest 1749: <dd><cite><a href="http://www.w3.org/TR/DOM-Level-3-Core">Document Object
1.92 avankest 1750: Model (DOM) Level 3 Core Specification</a></cite>, A. Le Hors, P. Le
1.139 avankest 1751: Hégaret, L. Wood, G. Nicol, J. Robie, M. Champion, S. Byrne,
1752: editors. W3C, April 2004.</dd>
1.11 avankest 1753:
1.117 avankest 1754: <dt>[<dfn id="ref-ecmascript">ECMAScript</dfn>]</dt>
1.92 avankest 1755: <dd><cite><a href="http://www.ecma-international.org/publications/standards/Ecma-262.htm">ECMAScript
1.248 avankest 1756: Language Specification</a></cite>, Third Edition. ECMA, December
1757: 1999.</dd>
1758:
1759:
1.171 avankest 1760:
1.145 avankest 1761: <dt>[<dfn id="ref-html5">HTML5</dfn>]</dt>
1.171 avankest 1762: <dd><cite><a href="http://www.w3.org/html/wg/html5/">HTML 5</a></cite>
1763: (work in progress), I. Hickson, D. Hyatt, editors. W3C, 2008.</dd>
1.142 avankest 1764: <dd><cite><a href="http://www.whatwg.org/specs/web-apps/current-work/">HTML 5</a></cite>
1.171 avankest 1765: (work in progress), I. Hickson, editor. WHATWG, 2008.</dd>
1.198 avankest 1766:
1767: <dt>[<dfn id="ref-httpverbsec">HTTPVERBSEC</dfn>]</dt>
1.248 avankest 1768: <dd>(Non-normative)
1769: <cite><a href="http://www.kb.cert.org/vuls/id/867593">Multiple vendors'
1770: web servers enable HTTP TRACE method by default</a></cite>, US-CERT.</dd>
1771: <dd>(Non-normative)
1772: <cite><a href="http://www.kb.cert.org/vuls/id/288308">Microsoft Internet
1773: Information Server (IIS) vulnerable to cross-site scripting via HTTP
1774: TRACK method</a></cite>, US-CERT.</dd>
1775: <dd>(Non-normative)
1776: <cite><a href="http://www.kb.cert.org/vuls/id/150227">HTTP proxy default
1777: configurations allow arbitrary TCP connections</a></cite>, US-CERT.</dd>
1778:
1779:
1780:
1781: <dt>[<dfn id="ref-rfc2046">RFC2046</dfn>]</dt>
1782: <dd><cite><a href="http://ietf.org/rfc/rfc2046">Multipurpose Internet Mail
1783: Extensions (MIME) Part Two: Media Types</a></cite>, N. Freed, N.
1784: Borenstein, editors. IETF, November 1996.</dd>
1.198 avankest 1785:
1.145 avankest 1786: <dt>[<dfn id="ref-rfc2119">RFC2119</dfn>]</dt>
1.117 avankest 1787: <dd><cite><a href="http://ietf.org/rfc/rfc2119">Key words for use in RFCs to
1788: Indicate Requirement Levels</a></cite>, S. Bradner. IETF, March 1997.</dd>
1.12 avankest 1789:
1.248 avankest 1790: <dt>[<dfn id="rfc-rfc2616">RFC2616</dfn>]</dt>
1.11 avankest 1791: <dd><cite><a href="http://ietf.org/rfc/rfc2616">Hypertext Transfer Protocol
1.92 avankest 1792: -- HTTP/1.1</a></cite>, R. Fielding, J. Gettys, J. Mogul, H. Frystyk, L.
1793: Masinter, P. Leach, T. Berners-Lee, editors. IETF, June 1999.</dd>
1.12 avankest 1794:
1.37 avankest 1795: <dt>[<dfn id="ref-rfc2617">RFC2617</dfn>]</dt>
1.11 avankest 1796: <dd><cite><a href="http://ietf.org/rfc/rfc2617">HTTP Authentication: Basic
1.92 avankest 1797: and Digest Access Authentication</a></cite>, P. Hallam-Baker, J.
1798: Hostetler, S. Lawrence, P. Leach, A. Luotonen, L. Stewart, editors. IETF,
1799: June 1999.</dd>
1.12 avankest 1800:
1.37 avankest 1801: <dt>[<dfn id="ref-rfc3986">RFC3986</dfn>]</dt>
1.11 avankest 1802: <dd><cite><a href="http://ietf.org/rfc/rfc3986">Uniform Resource Identifier
1.92 avankest 1803: (URI): Generic Syntax</a></cite>, T. Berners-Lee, R. Fielding, L. Masinter,
1804: editors. IETF, January 2005.</dd>
1.184 avankest 1805:
1.248 avankest 1806: <dt>[<dfn id=ref-rfc3987>RFC3987</dfn>]</dt>
1807: <dd><cite><a href="http://ietf.org/rfc/rfc3987">Internationalized
1808: Resource Identifiers (IRIs)</a></cite>, M. Duerst, M. Suignard, editors.
1809: IETF, January 2005. L. Masinter, editors. IETF, January 2005.</dd>
1810:
1.203 avankest 1811: <dt>[<dfn id="ref-webidl">WebIDL</dfn>]</dt>
1.200 avankest 1812: <dd><cite><a href="http://dev.w3.org/2006/webapi/WebIDL/">Web
1.203 avankest 1813: IDL</a></cite> (work in progress), C. McCormack, editor. W3C, 2009.</dd>
1.181 avankest 1814:
1.92 avankest 1815: <dt>[<dfn id="ref-xml">XML</dfn>]</dt>
1816: <dd><cite><a href="http://www.w3.org/TR/xml/">Extensible Markup Language
1.203 avankest 1817: (XML) 1.0 (Fifth Edition)</a></cite>, T. Bray, J. Paoli, C.
1818: Sperberg-McQueen, E. Maler, F. Yergeau, editors. W3C, November 2008.</dd>
1.92 avankest 1819: <dd><cite><a href="http://www.w3.org/TR/xml-names/">Namespaces in XML
1.117 avankest 1820: (Second Edition)</a></cite>, T. Bray, D. Hollander, A. Layman, R. Tobin,
1821: editors. W3C, August 2006.</dd>
1.1 avankest 1822: </dl>
1.11 avankest 1823:
1.203 avankest 1824:
1825:
1.130 avankest 1826: <h2 class="no-num" id="acknowledgments">Acknowledgments</h2>
1.11 avankest 1827:
1.159 avankest 1828: <p>The editor would like to thank
1.12 avankest 1829:
1.163 avankest 1830: Addison Phillips,
1.159 avankest 1831: Ahmed Kamel,
1832: Alex Hopmann,
1833: Alex Vincent,
1834: Alexey Proskuryakov,
1835: Asbjørn Ulsberg,
1836: Boris Zbarsky,
1837: Björn Höhrmann,
1838: Cameron McCormack,
1839: Christophe Jolif,
1840: Charles McCathieNevile,
1841: Dan Winship,
1.248 avankest 1842: David Andersson,
1.159 avankest 1843: David Håsäther,
1.248 avankest 1844: David Levin,
1.159 avankest 1845: Dean Jackson,
1846: Denis Sureau,
1847: Doug Schepers,
1848: Douglas Livingstone,
1849: Elliotte Harold,
1850: Eric Lawrence,
1.196 avankest 1851: Erik Dahlström,
1.163 avankest 1852: Geoffrey Sneddon,
1.159 avankest 1853: Gideon Cohn,
1854: Gorm Haug Eriksen,
1.247 avankest 1855: Håkon Wium Lie,
1.159 avankest 1856: Hallvord R. M. Steen,
1.247 avankest 1857: Huub Schaeks,
1.159 avankest 1858: Ian Davis,
1859: Ian Hickson,
1860: Ivan Herman,
1861: Jeff Walden,
1862: Jens Lindström,
1863: Jim Deegan,
1864: Jim Ley,
1865: Joe Farro,
1866: Jonas Sicking,
1867: Julian Reschke,
1868: Karl Dubost,
1.175 avankest 1869: Lachlan Hunt,
1.159 avankest 1870: Maciej Stachowiak,
1871: Magnus Kristiansen,
1872: Marc Hadley,
1873: Marcos Caceres,
1874: Mark Baker,
1.203 avankest 1875: Mark Birbeck,
1.159 avankest 1876: Mark Nottingham,
1.248 avankest 1877: Martin Hassman,
1.159 avankest 1878: Mohamed Zergaoui,
1.248 avankest 1879: Olly Pettay,
1.159 avankest 1880: Pawel Glowacki,
1.231 avankest 1881: Peter Michaux,
1.159 avankest 1882: Robin Berjon,
1.248 avankest 1883: Rune Halvorsen,
1.159 avankest 1884: Ruud Steltenpool,
1885: Simon Pieters,
1886: Stewart Brodie,
1887: Sunava Dutta,
1.186 avankest 1888: Thomas Roessler,
1.203 avankest 1889: Tom Magliery, and
1.159 avankest 1890: Zhenbin Xu
1.181 avankest 1891:
1.159 avankest 1892: for their contributions to this specification.</p>
1.12 avankest 1893:
1.1 avankest 1894: <p>Special thanks to the Microsoft employees who first implemented the
1.143 avankest 1895: <code title="">XMLHttpRequest</code> interface, which was first widely
1896: deployed by the Windows Internet Explorer browser.</p>
1.12 avankest 1897:
1.143 avankest 1898: <p>Special thanks also to the WHATWG for drafting an initial version of
1899: this specification in their Web Applications 1.0 document (now renamed to
1900: HTML 5). [<cite><span>HTML5</span></cite>]</p>
1.12 avankest 1901:
1.143 avankest 1902: <p>Thanks also to all those who have helped to improve this specification
1903: by sending suggestions and corrections. (Please, keep bugging us with your
1.22 avankest 1904: issues!)</p>
1.1 avankest 1905: </body>
1906: </html>
1.211 avankest 1907:
Webmaster