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