Annotation of 2006/webapi/XMLHttpRequest/Overview.src.html, revision 1.101
1.60 avankest 1: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
1.1 avankest 2: <html lang="en-US">
3: <head>
4: <title>The XMLHttpRequest Object</title>
1.16 avankest 5: <style type="text/css">
6: pre.idl { border:solid thin; background:#eee; color:#000; padding:0.5em }
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.89 avankest 12: .issue { padding:.5em; border:solid #f00 }
1.16 avankest 13: p.issue::before { content:"Issue: " }
14: em.ct { text-transform:lowercase; font-variant:small-caps; font-style:normal }
15: dfn { font-weight:bold; font-style:normal }
16: code { color:orangered }
17: code :link, code :visited { color:inherit }
18: </style>
1.7 avankest 19: <link rel="stylesheet" href="http://www.w3.org/StyleSheets/TR/base">
1.58 avankest 20: <!--<link rel="stylesheet" href="http://www.w3.org/StyleSheets/TR/W3C-WD">-->
1.1 avankest 21: </head>
22: <body>
23: <div class="head">
24: <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 25:
1.10 avankest 26: <h1 class="head">The <code title="">XMLHttpRequest</code> Object</h1>
1.12 avankest 27:
1.53 avankest 28: <h2 class="no-num no-toc" id="pagesubtitle">Editor's draft<!--W3C Working Draft--> [DATE: 3 August 2002]</h2>
1.12 avankest 29:
1.1 avankest 30: <dl>
1.10 avankest 31: <dt>This Version:</dt>
1.24 avankest 32:
1.47 avankest 33: <dd><a href="[VERSION]/">http://www.w3.org/TR/[YEAR]/WD-XMLHttpRequest-[CDATE]/</a></dd>
1.14 avankest 34:
1.10 avankest 35: <dt>Latest Version:</dt>
1.24 avankest 36:
1.1 avankest 37: <dd><a href="http://www.w3.org/TR/XMLHttpRequest/">http://www.w3.org/TR/XMLHttpRequest/</a></dd>
1.14 avankest 38:
1.10 avankest 39: <dt>Previous Versions:</dt>
1.24 avankest 40:
1.58 avankest 41: <dd><a href="http://www.w3.org/TR/2007/WD-XMLHttpRequest-20070227/">http://www.w3.org/TR/2007/WD-XMLHttpRequest-20070227/</a></dd>
42:
1.22 avankest 43: <dd><a href="http://www.w3.org/TR/2006/WD-XMLHttpRequest-20060927/">http://www.w3.org/TR/2006/WD-XMLHttpRequest-20060927/</a></dd>
1.24 avankest 44:
1.1 avankest 45: <dd><a href="http://www.w3.org/TR/2006/WD-XMLHttpRequest-20060619/">http://www.w3.org/TR/2006/WD-XMLHttpRequest-20060619/</a></dd>
1.24 avankest 46:
1.1 avankest 47: <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 48:
1.1 avankest 49: <dt>Editor:</dt>
1.24 avankest 50:
1.1 avankest 51: <dd><a href="http://annevankesteren.nl/">Anne van Kesteren</a>
52: (<a href="http://www.opera.com/">Opera Software ASA</a>)
53: <<a href="mailto:annevk@opera.com">annevk@opera.com</a>></dd>
54: </dl>
1.14 avankest 55:
1.1 avankest 56: <p class="copyright"><a href="http://www.w3.org/Consortium/Legal/ipr-notice#Copyright">Copyright</a>
1.51 avankest 57: © 2007
58: <a href="http://www.w3.org/"><acronym title="World Wide Web Consortium">W3C</acronym></a><sup>®</sup>
59: (<a href="http://www.csail.mit.edu/"><acronym title="Massachusetts Institute of Technology">MIT</acronym></a>,
60: <a href="http://www.ercim.org/"><acronym title="European Research Consortium for Informatics and Mathematics">ERCIM</acronym></a>,
61: <a href="http://www.keio.ac.jp/">Keio</a>), All Rights Reserved. W3C
62: <a href="http://www.w3.org/Consortium/Legal/ipr-notice#Legal_Disclaimer">liability</a>,
63: <a href="http://www.w3.org/Consortium/Legal/ipr-notice#W3C_Trademarks">trademark</a>
64: and
65: <a href="http://www.w3.org/Consortium/Legal/copyright-documents">document
66: use</a> rules apply.</p>
1.1 avankest 67: </div>
1.12 avankest 68:
1.1 avankest 69: <hr>
1.12 avankest 70:
1.1 avankest 71: <h2 class="no-num no-toc" id="specabstract">Abstract</h2>
1.10 avankest 72:
1.22 avankest 73: <p>The <code title="">XMLHttpRequest</code> Object specification defines an
74: <abbr title="Application Programming Interface">API</abbr> that provides
75: scripted client functionality for transferring data between a client and a
76: server.</p>
1.12 avankest 77:
1.1 avankest 78: <h2 class="no-num no-toc" id="sotd">Status of this Document</h2>
1.12 avankest 79:
1.1 avankest 80: <p><em>This section describes the status of this document at the time of its
1.36 avankest 81: publication. Other documents may supersede this document. A list of current
82: W3C publications and the latest revision of this technical report can be
83: found in the <a href="http://www.w3.org/TR/">W3C technical reports index</a>
84: at http://www.w3.org/TR/.</em></p>
85:
1.63 avankest 86: <!-- change back to Last Call -->
87: <p>This is the [DATE: 3 August 2002] <strong>Editor's</strong> Working
1.54 avankest 88: Draft of The <code title="">XMLHttpRequest</code> Object specification.
1.47 avankest 89: Please send comments to
90: <a href="mailto:public-webapi@w3.org">public-webapi@w3.org</a>
91: (<a href="http://lists.w3.org/Archives/Public/public-webapi/">archived</a>)
92: with either <samp>[XHR]</samp> or <samp title="">[XMLHttpRequest]</samp> at
1.63 avankest 93: the start of the subject line<!-- by 2 April 2007-->.</p>
1.51 avankest 94:
1.47 avankest 95: <p>This document is produced by the
96: <a href="http://www.w3.org/2006/webapi/">Web API Working Group</a>, part of
97: the <a href="http://www.w3.org/2006/rwc/Activity">Rich Web Clients
98: Activity</a> in the W3C <a href="http://www.w3.org/Interaction/">Interaction
1.51 avankest 99: Domain</a>. Changes made to this document can be found in the
1.47 avankest 100: <a href="http://dev.w3.org/cvsweb/2006/webapi/XMLHttpRequest/Overview.html">W3C
101: public CVS server</a>.</p>
1.12 avankest 102:
1.1 avankest 103: <p>Publication as a Working Draft does not imply endorsement by the W3C
1.36 avankest 104: Membership. This is a draft document and may be updated, replaced or
105: obsoleted by other documents at any time. It is inappropriate to cite this
106: document as other than work in progress.</p>
1.12 avankest 107:
1.1 avankest 108: <p>This document was produced by a group operating under the
1.36 avankest 109: <a href="http://www.w3.org/Consortium/Patent-Policy-20040205/">5 February
1.52 avankest 110: 2004 W3C Patent Policy</a>. W3C maintains a
1.36 avankest 111: <a rel="disclosure" href="http://www.w3.org/2004/01/pp-impl/38482/status">public
112: list of any patent disclosures</a> made in connection with the deliverables
113: of the group; that page also includes instructions for disclosing a patent.
114: An individual who has actual knowledge of a patent which the individual
115: believes contains
116: <a href="http://www.w3.org/Consortium/Patent-Policy-20040205/#def-essential">Essential
117: Claim(s)</a> must disclose the information in accordance with
118: <a href="http://www.w3.org/Consortium/Patent-Policy-20040205/#sec-Disclosure">section
119: 6 of the W3C Patent Policy</a>.</p>
1.12 avankest 120:
1.1 avankest 121: <h2 class="no-num no-toc" id="toc">Table of Contents</h2>
1.21 avankest 122:
1.1 avankest 123: <!--toc-->
1.12 avankest 124:
1.1 avankest 125: <h2 id="introduction">Introduction</h2>
1.12 avankest 126:
1.1 avankest 127: <p><em>This section is non-normative.</em></p>
1.12 avankest 128:
1.33 avankest 129: <p>The <code>XMLHttpRequest</code> object implements an interface exposed by
130: a scripting engine that allows scripts to perform HTTP client functionality,
1.22 avankest 131: such as submitting form data or loading data from a server.</p>
1.24 avankest 132:
1.1 avankest 133: <p>The name of the object is <code>XMLHttpRequest</code> for compatibility
1.22 avankest 134: with the web, though each component of this name is potentially misleading.
1.39 avankest 135: First, the object supports any text based format, including XML. Second, it
136: can be used to make requests over both HTTP and HTTPS (some implementations
137: support protocols in addition to HTTP and HTTPS, but that functionality is
138: not covered by this specification). Finally, it supports "requests" in a
139: broad sense of the term as it pertains to HTTP; namely all activity involved
140: with HTTP requests or responses for the defined HTTP methods.</p>
1.12 avankest 141:
1.1 avankest 142: <h3 id="examples">Examples of Usage</h3>
1.12 avankest 143:
1.1 avankest 144: <p><em>This section is non-normative.</em></p>
1.12 avankest 145:
1.37 avankest 146: <p>Some [<cite><span>ECMAScript</span></cite>] examples
1.32 avankest 147: are listed in the specification. In addition, you can find some below.</p>
1.14 avankest 148:
149: <div class="example">
150: <p>Some simple code to do something with data from an XML document fetched
1.22 avankest 151: over the network:</p>
1.16 avankest 152:
1.58 avankest 153: <pre><code>function test(data) {
1.14 avankest 154: // taking care of data
155: }
156:
157: function handler() {
158: if(this.readyState == 4 && this.status == 200) {
159: // so far so good
160: if(this.responseXML != null && this.responseXML.getElementById('test').firstChild.data)
161: // success!
162: test(this.responseXML.getElementById('test').firstChild.data);
163: else
164: test(null);
165: } else if (this.readyState == 4 && this.status != 200) {
166: // fetched the wrong page or network error...
167: test(null);
168: }
169: }
1.12 avankest 170:
1.14 avankest 171: var client = new XMLHttpRequest();
172: client.onreadystatechange = handler;
173: client.open("GET", "test.xml");
1.58 avankest 174: client.send();</code></pre>
1.16 avankest 175:
1.56 avankest 176: <p>If you just want to log a message to the server:</p>
1.16 avankest 177:
1.58 avankest 178: <pre><code>function log(message) {
1.14 avankest 179: var client = new XMLHttpRequest();
1.56 avankest 180: client.open("POST", "/log");
1.57 avankest 181: client.setRequestHeader("Content-Type", "text/plain;charset=UTF-8");
1.14 avankest 182: client.send(message);
1.58 avankest 183: }</code></pre>
1.16 avankest 184:
1.14 avankest 185: <p>Or if you want to check the status of a document on the server:</p>
1.16 avankest 186:
1.58 avankest 187: <pre><code>function fetchStatus(address) {
1.14 avankest 188: var client = new XMLHttpRequest();
189: client.onreadystatechange = function() {
190: // in case of network errors this might not give reliable results
191: if(this.readyState == 4)
192: returnStatus(this.status);
193: }
194: client.open("HEAD", address);
195: client.send();
1.58 avankest 196: }</code></pre>
1.14 avankest 197: </div>
1.12 avankest 198:
1.1 avankest 199: <h3 id="conformance">Conformance</h3>
1.12 avankest 200:
1.27 avankest 201: <p>Everything in this specification is normative except for diagrams,
202: examples, notes and sections marked non-normative.</p>
1.12 avankest 203:
1.1 avankest 204: <p>The key words <em class="ct">must</em>, <em class="ct">must not</em>,
1.74 avankest 205: <em class="ct">should</em> and <em class="ct">may</em> in this document are
206: to be interpreted as described in RFC 2119.
207: [<cite><span>RFC2119</span></cite>]</p>
1.12 avankest 208:
1.1 avankest 209: <p>This specification defines the following classes of products:</p>
1.12 avankest 210:
1.1 avankest 211: <dl>
1.74 avankest 212: <dt><dfn id="conforming-user-agent">Conforming user agent</dfn></dt>
1.24 avankest 213:
1.74 avankest 214: <dd>
215: <p>A user agent <em class="ct">must</em> behave as described in this
216: specification in order to be considered conformant even when faced with
217: non-conforming scripts.</p>
1.95 avankest 218:
1.94 avankest 219: <p>If the user agent does not support XML (including support for
220: namespaces) the <span>XML response entity body</span>
221: <em class="ct">must</em> (always) be <code>null</code>.</p>
1.74 avankest 222:
1.75 avankest 223: <p>User agents <em class="ct">may</em> optimize any algorithm given in
224: this specification, so long as the end result is indistinguishable from
225: the result that would be obtained by the specification's algorithms. (The
226: algorithms in this specification are generally written with more concern
227: for clarity than efficiency.)</p>
1.95 avankest 228:
229: <p class="note">This specification uses both the terms "conforming user
230: agent(s)" and "user agent(s)" to refer to this product class.</p>
1.74 avankest 231: </dd>
1.94 avankest 232:
233: <dt><dfn id="conforming-xml-user-agent">Conforming XML user agent</dfn></dt>
234:
235: <dd><p>A user agent that is a <span>conforming user agent</span> and also
236: supports XML (including support for namespaces).</p></dd>
1.24 avankest 237:
1.74 avankest 238: <dt><dfn id="conforming-script">Conforming script</dfn></dt>
1.24 avankest 239:
1.74 avankest 240: <dd><p>A script <em class="ct">must</em> satisfy the constraints and
241: conditions described by this specification in order to be
242: conformant.</p></dd>
1.1 avankest 243: </dl>
1.32 avankest 244:
1.30 avankest 245: <h4 id="dependencies">Dependencies</h4>
1.33 avankest 246:
1.29 avankest 247: <p>This specification relies on several underlying specifications.</p>
248: <dl>
249: <dt>DOM</dt>
1.33 avankest 250:
1.29 avankest 251: <dd>
1.95 avankest 252: <p><span title="Conforming user agent">Conforming user agents</span>
253: <em class="ct">must</em> support some version of DOM Events and DOM Core,
254: because this specification uses some of the features defined in those
255: specifications. [<cite><span>DOM3Events</span></cite>]
256: [<cite><span>DOM3Core</span></cite>]</p>
257:
258: <p>It <em class="ct">must</em> also support some version of the Window
259: Object because some of the functionality in this specification relies on
260: it. [<cite><span>Window</span></cite>]</p>
1.29 avankest 261: </dd>
262:
263: <dt>HTTP</dt>
1.33 avankest 264:
1.29 avankest 265: <dd>
1.95 avankest 266: <p><span title="Conforming user agent">Conforming user agents</span>
267: <em class="ct">must</em> support some version of the HTTP protocol. The
268: user agent <em class="ct">should</em> support any HTTP method that matches
269: the <span><code>Method</code> production</span> and
270: <em class="ct">must</em> at least support the following methods:</p>
1.58 avankest 271:
272: <ul>
273: <li><code>GET</code></li>
274: <li><code>POST</code></li>
275: <li><code>HEAD</code></li>
276: <li><code>PUT</code></li>
277: <li><code>DELETE</code></li>
278: <li><code>OPTIONS</code></li>
279: </ul>
280:
281: <p>Other requirements regarding HTTP are made throughout the
1.39 avankest 282: specification. [<cite><span>RFC2616</span></cite>]</p>
1.29 avankest 283: </dd>
1.41 avankest 284:
285: <dt>XML</dt>
286:
1.95 avankest 287: <dd><p><span title="Conforming XML user agent">Conforming XML user
288: agents</span> <em class="ct">must</em> support XML (including support for
289: namespaces). [<cite><span>XML</span></cite>]
290: [<cite><span>XMLNS</span></cite>]</p></dd>
1.29 avankest 291: </dl>
1.12 avankest 292:
1.80 avankest 293: <h4 id="terminology">Terminology</h4>
1.12 avankest 294:
1.80 avankest 295: <p>There is a <dfn id="case-insensitive-match">case-insensitive match</dfn>
1.101 ! avankest 296: of strings <var>s1</var> and <var>s2</var> if after uppercasing both strings
1.100 avankest 297: (by mapping a-z to A-Z) they are identical.</p>
1.80 avankest 298:
299: <h4 id="extensibility">Extensibility</h4>
1.12 avankest 300:
1.81 avankest 301: <p>Extensions of the API defined by this specification are <em>strongly
1.22 avankest 302: discouraged</em>. User agents, Working Groups and other interested parties
1.33 avankest 303: should discuss extensions on a relevant public forum, preferably
1.22 avankest 304: <a href="mailto:public-webapi@w3.org">public-webapi@w3.org</a>.</p>
1.12 avankest 305:
306: <h2 id="xmlhttprequest">The <code title="">XMLHttpRequest</code> Object</h2>
307:
1.58 avankest 308: <p>The <code>XMLHttpRequest</code> object can be used by scripts to
1.22 avankest 309: programmatically connect to their originating server via HTTP.</p>
1.12 avankest 310:
1.1 avankest 311: <p>Objects implementing the <code>XMLHttpRequest</code> interface
1.22 avankest 312: <em class="ct">must</em> also implement the <code>EventTarget</code>
1.58 avankest 313: interface. [<cite><span>DOM3Events</span></cite>]</p>
1.12 avankest 314:
1.58 avankest 315: <p>Objects implementing the <code title="">Window</code> interface
316: <em class="ct">must</em> provide an <code title="">XMLHttpRequest()</code>
317: constructor. [<cite><span>Window</span></cite>]</p>
1.12 avankest 318:
1.1 avankest 319: <div class="example">
1.58 avankest 320: <p>In ECMAScript this can be used as follows:</p>
321: <!-- [<cite><span>ECMAScript</span></cite>] -->
322:
323: <pre><code>var client = new XMLHttpRequest();</code></pre>
1.1 avankest 324: </div>
1.12 avankest 325:
1.58 avankest 326: <p>When the <code title="">XMLHttpRequest()</code> constructor is invoked a
327: persistent pointer to the associated <code title="">Window</code> object
328: <em class="ct">must</em> be stored on the newly created object. This is the
1.25 avankest 329: <dfn title="Window pointer" id="window-pointer"><code>Window</code>
1.58 avankest 330: pointer</dfn>. The associated <code title="">Window</code> object is the one
331: of which the <code title="">XMLHttpRequest</code> constructor was
332: invoked. This <span title="Window pointer">pointer</span>
1.25 avankest 333: <em class="ct">must</em> persist even if the browsing context in which the
1.37 avankest 334: <code title="">Window</code> is located is destroyed (by removing it from a
335: parent browsing context, for instance).</p>
1.25 avankest 336:
1.37 avankest 337: <p>The term browsing context is defined by the <cite>Window Object
338: 1.0</cite> specification. [<cite><span>Window</span></cite>]</p>
1.27 avankest 339:
340: <!-- XXX if the document object changes in the browsing context you get an
1.33 avankest 341: exception in some implementations. -->
1.27 avankest 342:
1.58 avankest 343: <div class="example">
1.65 avankest 344: <p>If <var><code>win</code></var> is a <code title="">Window</code> object
345: <var><code>client</code></var> will have a pointer to
346: <var><code>win</code></var> in the following example:</p>
1.58 avankest 347:
1.65 avankest 348: <pre><code>var client = new win.XMLHttpRequest()</code></pre>
1.58 avankest 349: </div>
1.12 avankest 350:
1.58 avankest 351: <pre class="idl">interface <dfn id="xmlhttprequest-object">XMLHttpRequest</dfn> {
352: // event handler
1.70 avankest 353: attribute EventListener <span>onreadystatechange</span>;
1.58 avankest 354:
355: // state
1.71 avankest 356: const unsigned short <span title="UNSENT state">UNSENT</span> = 0;
1.58 avankest 357: const unsigned short <span title="OPEN state">OPEN</span> = 1;
358: const unsigned short <span title="SENT state">SENT</span> = 2;
1.86 avankest 359: const unsigned short <span title="LOADING state">LOADING</span> = 3;
1.79 avankest 360: const unsigned short <span title="DONE state">DONE</span> = 4;
1.70 avankest 361: readonly attribute unsigned short <span>readyState</span>;
1.58 avankest 362:
363: // request
1.70 avankest 364: void <span>open</span>(in DOMString <var>method</var>, in DOMString <var>url</var>);
365: void <span>open</span>(in DOMString <var>method</var>, in DOMString <var>url</var>, in boolean <var>async</var>);
366: void <span>open</span>(in DOMString <var>method</var>, in DOMString <var>url</var>, in boolean <var>async</var>, in DOMString <var>user</var>);
367: void <span>open</span>(in DOMString <var>method</var>, in DOMString <var>url</var>, in boolean <var>async</var>, in DOMString <var>user</var>, in DOMString <var>password</var>);
368: void <span>setRequestHeader</span>(in DOMString <var>header</var>, in DOMString <var>value</var>);
369: void <span>send</span>();
370: void <span>send</span>(in DOMString <var>data</var>);
371: void <span>send</span>(in Document <var>data</var>);
372: void <span>abort</span>();
1.58 avankest 373:
374: // response
1.70 avankest 375: DOMString <span>getAllResponseHeaders</span>();
376: DOMString <span>getResponseHeader</span>(in DOMString <var>header</var>);
377: readonly attribute DOMString <span>responseText</span>;
378: readonly attribute Document <span>responseXML</span>;
379: readonly attribute unsigned short <span>status</span>;
380: readonly attribute DOMString <span>statusText</span>;
1.1 avankest 381: };</pre>
1.8 avankest 382:
1.58 avankest 383: <p>The <code>XMLHttpRequest</code> object can be in five states:
1.71 avankest 384: <span title="UNSENT state">UNSENT</span>,
1.58 avankest 385: <span title="OPEN state">OPEN</span>, <span title="SENT state">SENT</span>,
1.86 avankest 386: <span title="LOADING state">LOADING</span> and
1.79 avankest 387: <span title="DONE state">DONE</span>. The current state is exposed through
388: the <code>readyState</code> attribute. The method definitions below define
389: when a state transition takes place.</p>
1.58 avankest 390:
391: <p>When constructed, the <code>XMLHttpRequest</code> object
1.71 avankest 392: <em class="ct">must</em> be in the UNSENT state. This state is represented
393: by the <dfn id="unsent" title="UNSENT state"><code>UNSENT</code></dfn>
1.58 avankest 394: constant, whose value is <code>0</code>.</p>
395:
396: <p>The OPEN state is the state of the object when the <code>open()</code>
1.92 avankest 397: method has been successfully invoked. During this state request headers can
1.58 avankest 398: be set using <code>setRequestHeader()</code> and the request can be made
399: using <code>send()</code>. This state is represented by the
400: <dfn id="open" title="OPEN state"><code>OPEN</code></dfn> constant, whose
401: value is <code>1</code>.</p>
402:
1.87 avankest 403: <p>During the OPEN state there is a <dfn id="send-flag"><code>send()</code>
404: flag</dfn> which influences several algorithms described below. Outside the
405: OPEN state its value <em class="ct">must</em> (always) be "false". During
406: the OPEN state it can be set to "true" as described in the
407: <code>send()</code> method algorithm.</p>
408:
1.58 avankest 409: <p>The SENT state is the state of the object when the user agent
410: successfully acknowledged the request. This state is represented by the
411: <dfn id="sent" title="SENT state"><code>SENT</code></dfn> constant, whose
412: value is <code>2</code>.</p>
413:
1.86 avankest 414: <p>The LOADING state is the state of the object when all HTTP headers have
1.58 avankest 415: been received. The object typically remains in this state until the complete
416: message body (if any) has been received. This state is represented by the
1.86 avankest 417: <dfn id="loading" title="LOADING state"><code>LOADING</code></dfn> constant,
418: whose value is <code>3</code>.</p>
1.58 avankest 419:
1.79 avankest 420: <p>The DONE state is the state of the object when either the data transfer
1.58 avankest 421: has been completed or something went wrong during the transfer (infinite
422: redirects for instance). This state is represented by the
1.79 avankest 423: <dfn id="done" title="DONE state"><code>DONE</code></dfn> constant, whose
424: value is <code>4</code>.</p>
1.90 avankest 425:
1.88 avankest 426: <p>The <dfn id="response-entity-body">response entity body</dfn> is the
427: fragment of the <span>entity body</span> received so far (LOADING state) or
428: the complete entity body (DONE state). If there is no entity body the
429: response entity body is "null".</p>
430:
431: <p>The <dfn id="text-response-entity-body">text response entity body</dfn>
1.90 avankest 432: is either a <code>DOMString</code> or <code>null</code>. The value of the
1.96 avankest 433: text response entity body <em class="ct">must</em> be determined by running
434: the following algorithm:</p>
1.90 avankest 435:
1.88 avankest 436: <ol>
1.96 avankest 437: <li><p>If the <span>response entity body</span> is "null" return
438: <code>null</code> and terminate these steps.</p>
1.90 avankest 439:
440: <li><p>Let <var>charset</var> be UTF-8.</p></li>
441:
442: <li><p>If there is no <code>Content-Type</code> header or there is a
443: <code>Content-Type</code> header which contains a MIME type that is
1.91 avankest 444: <code>text/xml</code>, <code>application/xml</code>, <code>text/xsl</code>
445: or ends in <code title="">+xml</code> (ignoring any parameters) use the
446: rules set forth in the XML specification to determine the character
447: encoding. Let <var>charset</var> be the determined character
448: encoding.</p></li>
1.90 avankest 449:
450: <li><p>If the <code>Content-Type</code> MIME type contains a
451: <code>charset</code> parameter let <var>charset</var> be the value of that
452: parameter.</p></li>
453:
1.96 avankest 454: <li><p>Return the result of decoding the <span>response entity body</span>
455: using <var>charset</var>. Or, if that fails, return
1.90 avankest 456: <code>null</code>.</p></li>
1.88 avankest 457: </ol>
1.90 avankest 458:
1.88 avankest 459: <p>If the <span>response entity body</span> is "null" the text response
460: entity body is <code>null</code>. If the user agent fails to decode the
461: response entity body the text response entity body is also
462: <code>null</code>.</p>
1.90 avankest 463:
1.88 avankest 464: <p>The <dfn id="xml-response-entity-body">XML response entity body</dfn> is
465: a <code>Document</code> representing the response entity body or
1.96 avankest 466: <code>null</code> and is determined by running the following algorithm
467: (the return value is its value):</p>
1.90 avankest 468:
1.88 avankest 469: <ol>
1.96 avankest 470: <li><p>If the <span>response entity body</span> is "null" terminate these
471: steps and return <code>null</code>.</p></li>
1.88 avankest 472:
1.90 avankest 473: <li><p>If the <code>Content-Type</code> header does not contain a MIME type
474: (ignoring any parameters) that is <code>text/xml</code>,
1.91 avankest 475: <code>application/xml</code>, <code>text/xsl</code> or ends in
1.96 avankest 476: <code title="">+xml</code> terminate these steps and return
1.99 avankest 477: <code>null</code>. (Do not terminate these steps if there is no
478: <code>Content-Type</code> header at all.)</p></li>
1.88 avankest 479:
480: <li><p>Parse the <span>response entity body</span> following the rules from
481: the XML specifications. Let the result be <var>parsed document</var>. If
482: this fails (unsupported character encoding, namespace well-formedness error
1.96 avankest 483: et cetera) terminate these steps return <code>null</code>.
484: [<cite><span>XML</span></cite>] [<cite><span>XMLNS</span></cite>]</p></li>
1.88 avankest 485:
1.96 avankest 486: <li><p>Return an object implementing the <code>Document</code>
1.88 avankest 487: interface representing the <var>parsed document</var>.</p></li>
488: </ol>
1.58 avankest 489:
1.5 avankest 490: <dl>
1.12 avankest 491:
1.39 avankest 492: <dt><dfn id="dfn-onreadystatechange"><code>onreadystatechange</code></dfn>
493: of type <code>EventListener</code></dt>
1.12 avankest 494:
1.1 avankest 495: <dd>
496: <p>An attribute that takes an <code>EventListener</code> as value that
1.58 avankest 497: <em class="ct">must</em> be invoked along with any other appropriate event
498: listeners that are registered on this object when a
1.87 avankest 499: <code>readystatechange</code> event is dispatched on it during the
500: bubbling phase. Its initial value <em class="ct">must</em> be
501: <code>null</code>.</p>
1.1 avankest 502: </dd>
1.12 avankest 503:
1.1 avankest 504: <dt><dfn id="dfn-readystate"><code>readyState</code></dfn> of type
1.22 avankest 505: <code>unsigned short</code>, readonly</dt>
1.12 avankest 506:
1.1 avankest 507: <dd>
1.58 avankest 508: <p>The attribute <em class="ct">must</em> be the value of the constant
509: corresponding to the object's current state.</p>
1.1 avankest 510: </dd>
1.12 avankest 511:
1.14 avankest 512: <dt><dfn id="dfn-open" title="open"><code>open(<var>method</var>,
1.22 avankest 513: <var>url</var>, <var>async</var>, <var>user</var>,
514: <var>password</var>)</code></dfn>, method</dt>
1.12 avankest 515:
1.1 avankest 516: <dd>
1.58 avankest 517: <p>When invoked, the user agent <em class="ct">must</em> follow the
1.96 avankest 518: following steps (unless otherwise indicated):</p>
1.58 avankest 519:
520: <ol>
1.97 avankest 521: <li><p>If the <var>method</var> argument does not match the
1.58 avankest 522: <dfn><code>Method</code> production</dfn> defined in section 5.1.1 of RFC
1.98 avankest 523: 2616 raise a <code>SYNTAX_ERR</code> exception and terminate these steps.
1.58 avankest 524: [<cite><span>RFC2616</span></cite>]</p></li>
525:
526: <li><p>If the given <var>method</var> is not supported for security
527: reasons the user agent <em class="ct">should</em> raise a
1.98 avankest 528: <code>SECURITY_ERR</code> exception and terminate these steps.</p></li>
1.58 avankest 529:
530: <li><p>Let <var>stored method</var> be <var>method</var>.</p></li>
531:
1.80 avankest 532: <li><p>If <var>method</var>
533: <span title="case-insensitive match">case-insensitively matches</span>
534: <code>GET</code>, <code>POST</code>, <code>HEAD</code>, <code>PUT</code>,
1.96 avankest 535: <code>DELETE</code> or <code>OPTIONS</code> convert it to its uppercase
536: equivalent (by mapping a-z to A-Z) and let <var>stored method</var> be
537: the result.</p></li>
1.68 avankest 538: <!-- WebKit (and supposedly Firefox) also uppercase: CONNECT, COPY,
539: INDEX, LOCK, M-POST, MKCOL, MOVE, PROPFIND, PROPPATCH, TRACE and UNLOCK.
540: Not sure if TRACE and CONNECT are worth it though. They raise a
541: SECURITY_ERR exception anyway... -->
1.58 avankest 542:
543: <li><p>Drop the fragment identifier (if any) from <var>url</var> and let
544: <var>stored url</var> be the result of that operation.</p></li>
545:
1.96 avankest 546: <li><p>If <var>stored url</var> is a relative reference resolve it using
547: the current value of the <code>baseURI</code> attribute of the
548: <code>Document</code> object currently associated with the
549: <span><code>Window</code> pointer</span>. If this fails raise a
1.98 avankest 550: <code>SYNTAX_ERR</code> exception and terminate these steps.</p></li>
1.58 avankest 551:
1.101 ! avankest 552: <li><p>If <var>stored url</var> contains an unsupported scheme raise a
! 553: <code>NOT_SUPPORTED_ERR</code> and terminate these steps.</p></li>
! 554:
1.58 avankest 555: <li><p>If the <code>"user:password"</code> format in the
556: <code>userinfo</code> production defined in section 3.2.1 of RFC 3986 is
557: not supported for the relevant scheme and <var>stored url</var> contains
1.98 avankest 558: this format raise a <code>SYNTAX_ERR</code> and terminate these steps.
1.69 avankest 559: [<cite><span>RFC3986</span></cite>]</p></li>
1.58 avankest 560:
561: <li><p>If <var>stored url</var> contains the <code>"user:password"</code>
562: format let <var>stored user</var> be the user part and <var>stored
563: password</var> be the password part.</p></li>
564:
565: <li><p>If <var>stored url</var> just contains the <code>"user"</code>
566: format let <var>stored user</var> be the user part.</p></li>
567:
1.69 avankest 568: <li>
569: <p>If <var>stored url</var> is non same-origin the user agent
570: <em class="ct">should</em> raise a <code>SECURITY_ERR</code> exception
1.98 avankest 571: and terminate these steps.</p>
1.69 avankest 572: <p class="note">This specification does not define what constitutes as
1.71 avankest 573: non same-origin.<!-- XXX reference HTML5? --></p>
1.69 avankest 574: </li>
1.58 avankest 575:
576: <li><p>Let <var>async</var> be the value of the <var>async</var> argument
577: or <code>false</code> if it was omitted.</p></li>
578:
579: <li><p>If the <var>user</var> argument was not omitted and its syntax
1.96 avankest 580: does not match that specified by the relevant authentication scheme raise
1.98 avankest 581: a <code>SYNTAX_ERR</code> exception and terminate these steps.</p></li>
1.14 avankest 582:
1.58 avankest 583: <li>
584: <p>If the <var>user</var> argument was not omitted and is not
585: <code>null</code> let <var>stored user</var> be <var>user</var> encoded
586: using the encoding specified in the relevant authentication scheme or
587: UTF-8 if the scheme fails to specify an encoding.</p>
1.14 avankest 588:
1.58 avankest 589: <p class="note">This step overrides any user that may have been set by
590: the <var>url</var> argument.</p>
591: </li>
1.16 avankest 592:
1.58 avankest 593: <li><p>If the <var>user</var> argument was not omitted and is
594: <code>null</code> remove <var>stored user</var>.</p></li>
1.14 avankest 595:
1.58 avankest 596: <li><p>If the <var>password</var> argument was not omitted and its syntax
1.96 avankest 597: does not match that specified by the relevant authentication scheme raise
1.98 avankest 598: a <code>SYNTAX_ERR</code> exception and terminate these steps.</p></li>
1.58 avankest 599:
600: <li><p>If the <var>password</var> argument was not omitted and is not
601: <code>null</code> let <var>stored password</var> be <var>password</var>
602: encoded using the encoding specified in the relevant authentication
603: scheme or UTF-8 if the scheme fails to specify an encoding.</p></li>
604:
605: <li><p>If the <var>password</var> argument was not omitted and is
606: <code>null</code> remove <var>stored password</var>.</p></li>
607:
1.89 avankest 608: <li><p>Set <span>response entity body</span> to "null" and reset the list
609: of request headers.<p></li>
1.58 avankest 610:
1.60 avankest 611: <li><p>Any network activity for which the object responsible
612: <em class="ct">should</em> be cancelled.</p></li>
1.58 avankest 613: <!-- we can hardly require it... -->
614:
615: <li><p>Switch the object to the <span title="OPEN state">OPEN</span>
1.96 avankest 616: state and then synchronously dispatch a <code>readystatechange</code>
617: event on the object and return the method call.</p></li>
1.58 avankest 618: </ol>
1.14 avankest 619:
1.19 avankest 620: <p class="note">A future version or extension of this specification will
1.33 avankest 621: most likely define a way of doing cross-site requests.</p>
1.1 avankest 622: </dd>
1.12 avankest 623:
1.14 avankest 624: <dt><dfn id="dfn-setrequestheader" title="setrequestheader"><code>setRequestHeader(<var>header</var>,
1.22 avankest 625: <var>value</var>)</code></dfn>, method</dt>
1.12 avankest 626:
1.5 avankest 627: <dd>
1.45 avankest 628: <p>Each request has a list of request headers with associated values. This
629: method can be used to manipulate those values and set new request
630: headers.</p>
631:
1.58 avankest 632: <p>When invoked, the user agent <em class="ct">must</em> follow the
1.96 avankest 633: following steps (unless otherwise indicated):</p>
1.14 avankest 634:
1.58 avankest 635: <ol>
636: <li><p>If the state of the object is not
1.96 avankest 637: <span title="OPEN state">OPEN</span> raise an
1.98 avankest 638: <code>INVALID_STATE_ERR</code> exception and terminate these
639: steps.</p></li>
1.96 avankest 640:
641: <li><p>If the <span><code>send()</code> flag</span> is "true" raise an
1.98 avankest 642: <code>INVALID_STATE_ERR</code> exception and terminate these
643: steps.</p></li>
1.14 avankest 644:
1.97 avankest 645: <li><p>If the <var>header</var> argument does not match the
1.22 avankest 646: <dfn><code>field-name</code> production</dfn> as defined by section 4.2
1.96 avankest 647: of RFC 2616 or is <code>null</code> raise a <code>SYNTAX_ERR</code>
1.98 avankest 648: exception and terminate these steps.
1.96 avankest 649: [<cite><span>RFC2616</span></cite>]</p></li>
650:
1.98 avankest 651: <li><p>If the <var>value</var> argument is <code>null</code> terminate
652: these steps. (Do not raise an exception.)</li>
1.70 avankest 653: <!-- undefined is to be treated as null here... -->
1.79 avankest 654:
1.97 avankest 655: <li><p>If the <var>value</var> argument does not match the
1.22 avankest 656: <dfn><code>field-value</code> production</dfn> as defined by section 4.2
1.98 avankest 657: of RFC 2616 raise a <code>SYNTAX_ERR</code> and terminate these steps.
1.58 avankest 658: [<cite><span>RFC2616</span></cite>]</p></li>
1.14 avankest 659:
1.32 avankest 660: <li>
1.58 avankest 661: <p>For security reasons these steps <em class="ct">should</em> be
1.98 avankest 662: terminated if the <var>header</var> argument
1.80 avankest 663: <span title="case-insensitive match">case-insensitively matches</span>
664: one of the following headers:</p>
1.58 avankest 665:
1.32 avankest 666: <ul>
667: <li><code>Accept-Charset</code></li>
668: <li><code>Accept-Encoding</code></li>
1.68 avankest 669: <li><code>Connection</code></li>
1.32 avankest 670: <li><code>Content-Length</code></li>
1.68 avankest 671: <li><code>Content-Transfer-Encoding</code></li>
672: <li><code>Date</code></li>
1.32 avankest 673: <li><code>Expect</code></li>
674: <li><code>Host</code></li>
675: <li><code>Keep-Alive</code></li>
676: <li><code>Referer</code></li>
677: <li><code>TE</code></li>
678: <li><code>Trailer</code></li>
679: <li><code>Transfer-Encoding</code></li>
680: <li><code>Upgrade</code></li>
1.68 avankest 681: <li><code>Via</code></li>
1.32 avankest 682: </ul>
683: </li>
1.5 avankest 684:
1.58 avankest 685: <li><p>If the <var>header</var> argument is not in the list of request
1.96 avankest 686: headers append the <var>header</var> with its associated <var>value</var>
1.98 avankest 687: to the list and terminate these steps.</p></li>
1.58 avankest 688:
689: <li><p>If the <var>header</var> argument is in the list of request
1.96 avankest 690: headers either use multiple headers, combine the values or use a
691: combination of those (section 4.2, RFC 2616).
692: [<cite><span>RFC2616</span></cite>]</p></li>
693: <!-- XXX it seems UAs always combine the values -->
1.58 avankest 694: </ol>
1.45 avankest 695:
696: <p class="note">See also the <code>send()</code> method regarding user
1.71 avankest 697: agent header handling for caching, authentication, proxies, and
698: cookies.</p>
1.14 avankest 699:
700: <div class="example">
1.58 avankest 701: <pre><code>// The following script:
1.14 avankest 702: var client = new XMLHttpRequest();
703: client.open('GET', 'demo.cgi');
704: client.setRequestHeader('X-Test', 'one');
705: client.setRequestHeader('X-Test', 'two');
706: client.send();
707:
708: // ...would result in the following header being sent:
709: ...
710: X-Test: one, two
1.58 avankest 711: ...</code></pre>
1.14 avankest 712: </div>
1.5 avankest 713: </dd>
1.12 avankest 714:
1.14 avankest 715: <dt><dfn id="dfn-send" title="send"><code>send(<var>data</var>)</code></dfn>,
1.22 avankest 716: method</dt>
1.12 avankest 717:
1.17 avankest 718: <dd>
1.58 avankest 719: <p>The <code>send()</code> method initiates the request and its optional
720: argument <em class="ct">may</em> be used by scripts to provide the entity
721: body. When the argument is provided scripts <em class="ct">should</em>
722: ensure that they have specified (before invoking <code>send()</code>) the
1.60 avankest 723: <code>Content-Type</code> header via <code>setRequestHeader()</code>.</p>
1.58 avankest 724:
725: <p>When invoked, the user agent <em class="ct">must</em> follow the
1.96 avankest 726: following steps (unless otherwise noted):</p>
1.58 avankest 727:
728: <ol>
729: <li><p>If the state of the object is not
1.96 avankest 730: <span title="OPEN state">OPEN</span> raise an
1.98 avankest 731: <code>INVALID_STATE_ERR</code> exception and terminate these
732: steps.</p></li>
1.96 avankest 733:
734: <li><p>If the <span><code>send()</code> flag</span> is "true" raise an
1.98 avankest 735: <code>INVALID_STATE_ERR</code> exception and terminate these
736: steps.</p></li>
1.58 avankest 737:
738: <li>
739: <p>If the <var>data</var> argument has not been omitted and is not
1.96 avankest 740: <code>null</code> use it for the <dfn id="dfn-entity-body">entity
741: body</dfn> as defined by section 7.2 of RFC 2616 observing the following
742: rules: [<cite><span>RFC2616</span></cite>]</p>
1.58 avankest 743:
744: <dl>
745: <dt><var>data</var> is a <code>DOMString</code></dt>
746:
1.96 avankest 747: <dd><p>Encode <var>data</var> using UTF-8 for transmission.</p></dd>
1.58 avankest 748:
749: <dt><var>data</var> is a <code>Document</code>
750:
751: <dd>
1.96 avankest 752: <p>Serialize <var>data</var> into a namespace well-formed XML document
753: and encoded using the encoding given by
754: <code><var>data</var>.xmlEncoding</code>, if specified, or UTF-8
755: otherwise. Or, if this fails because the <code>Document</code>
756: cannot be serialized act as if <var>data</var> is
757: <code>null</code>.</p>
758:
759: <p>If no <code>Content-Type</code> header is in the list of request
760: headers append it to the list of request headers with a value of
761: <code>application/xml</code>.</p>
1.58 avankest 762:
763: <p class="note">Subsequent changes to the <code>Document</code> have
764: no effect on what is submitted.</p>
765: </dd>
766:
767: <dt><var>data</var> is not a <code>DOMString</code> or
768: <code>Document</code></dt>
769:
1.96 avankest 770: <dd><p>Use the stringification mechanisms of the host language on
771: <var>data</var> and treat the result as if <var>data</var> is a
772: <code>DOMString</code>. Or, if this fails, act as if <var>data</var> is
773: <code>null</code>.</p></dd>
1.58 avankest 774: </dl>
775:
776: <p>If the data argument has been omitted or is <code>null</code> no
1.96 avankest 777: entity body is used in the request.</p>
1.58 avankest 778: </li>
779:
1.71 avankest 780: <li><p>Make a request to <var>stored url</var>, using HTTP method
781: <var>stored method</var>, user <var>stored user</var> (if provided) and
782: password <var>stored password</var> (if provided), taking into account
783: the entity body, list of request headers and the rules listed directly
784: after this set of steps.</p></li>
1.60 avankest 785:
1.67 avankest 786: <li>
1.96 avankest 787: <p>Synchronously dispatch a <code>readystatechange</code> event on the
788: object.</p>
1.67 avankest 789:
1.96 avankest 790: <p class="note">The state of the object does not change. The event is
791: dispatched for historical reasons.</p>
1.67 avankest 792: </li>
793:
1.96 avankest 794: <li><p>If <var>async</var> is <code>true</code> set the
795: <dfn><code>send()</code> flag</dfn> to "true" and return the
1.98 avankest 796: <code>send()</code> method call. (Do not terminate the steps in the
797: algorithm though.)</p></li>
1.58 avankest 798:
1.82 avankest 799: <li><p>If the request has been successfully acknowledged synchronously
1.96 avankest 800: switch the state to <span title="SENT state">SENT</span> and then
801: synchronously dispatch a <code>readystatechange</code> event on the
802: object.</p></li>
1.58 avankest 803:
1.101 ! avankest 804: <li><p>If all HTTP headers have been received, immediately before
1.82 avankest 805: receiving the message body (if any), synchronously switch the state to
1.96 avankest 806: <span title="LOADING state">LOADING</span> and then synchronously
807: dispatch a <code>readystatechange</code> event on the object.</p></li>
1.58 avankest 808:
809: <li>
810: <p>If the response is an HTTP redirect (status code <code>301</code>,
1.96 avankest 811: <code>302</code>, <code>303</code> or <code>307</code>), transparently
812: follow it (unless it violates security, infinite loop precautions or the
813: scheme is not supported).</p>
1.60 avankest 814:
1.58 avankest 815: <p class="note">HTTP places requirements on the user agent regarding the
816: preservation of the request method and entity body during redirects, and
817: also requires users to be notified of certain kinds of automatic
818: redirections.</p>
819: <!-- Arguably HTTP should be fixed for the latter case. No browser
820: follows that as far as I know. -->
821: </li>
1.60 avankest 822:
1.58 avankest 823: <li>
1.96 avankest 824: <p>If something goes wrong (infinite loop, network errors) run the
825: following set of steps:</p>
1.60 avankest 826:
1.58 avankest 827: <ol>
1.96 avankest 828: <li><p>Set <span>response entity body</span> to "null" and reset the
1.89 avankest 829: list of request headers.</p></li>
1.60 avankest 830:
1.82 avankest 831: <li><p>Synchronously switch the state to
1.79 avankest 832: <span title="DONE state">DONE</span>.</p></li>
1.60 avankest 833:
1.96 avankest 834: <li><p>If <var>async</var> is set to <code>false</code> raise a
1.99 avankest 835: <code>NETWORK_ERR</code> exception and terminate the overall
836: algorithm.</p></li>
1.60 avankest 837:
1.58 avankest 838: <li><p>Synchronously dispatch a <code>readystatechange</code> event on
839: the object.</p></li>
1.60 avankest 840:
1.98 avankest 841: <li><p>Terminate the overall algorithm.</p></li>
1.58 avankest 842: </ol>
1.60 avankest 843:
1.82 avankest 844: <p class="note">It is likely that a future version of the
845: <code>XMLHttpRequest</code> object will dispatch an <code>error</code>
846: event here as well.</p>
1.58 avankest 847: </li>
848:
1.82 avankest 849: <li><p>When the request has successfully completed loading, synchronously
1.96 avankest 850: switch the state to <span title="DONE state">DONE</span> and then
851: synchronously dispatch a <code>readystatechange</code> event on the
852: object and return the method call in case of <var>async</var> being
853: <code>false</code>.</p></li>
1.58 avankest 854: </ol>
1.14 avankest 855:
1.91 avankest 856: <p>If the user agent allows the user to configure a proxy it
1.22 avankest 857: <em class="ct">should</em> modify the request appropriately;
1.42 avankest 858: <abbr title="in other words">i.e.</abbr>, connect to the proxy host
859: instead of the origin server, modify the <code>Request-Line</code> and
860: send <code>Proxy-Authorization</code> headers as specified.</p>
1.12 avankest 861:
1.42 avankest 862: <p>If the user agent supports HTTP Authentication it
1.22 avankest 863: <em class="ct">should</em> consider requests originating from this object
1.42 avankest 864: to be part of the protection space that includes the accessed URIs and
865: send <code>Authorization</code> headers and handle <code>401
1.72 avankest 866: Unauthorised</code> requests appropriately. If authentication fails, user
1.42 avankest 867: agents <em class="ct">should</em> prompt the users for credentials.
868: [<cite><span>RFC2617</span></cite>]</p>
869:
1.57 avankest 870: <p>If the user agent supports HTTP State Management it
1.42 avankest 871: <em class="ct">should</em> persist, discard and send cookies (as received
872: in the <code>Set-Cookie</code> and <code>Set-Cookie2</code> response
873: headers, and sent in the <code>Cookie</code> header) as applicable.
874: [<cite><span>RFC2965</span></cite>]</p>
875:
876: <p>If the user agent implements a HTTP cache it <em class="ct">should</em>
877: respect <code>Cache-Control</code> request headers set by the script
878: (<abbr title="for example">e.g.</abbr>, <code>Cache-Control:
879: no-cache</code> bypasses the cache). It <em class="ct">must not</em> send
880: <code>Cache-Control</code> or <code>Pragma</code> request headers
881: automatically unless the user explicitly requests such behaviour
882: (<abbr>e.g.</abbr>, by (force-)reloading the page). <code>304 Not
1.22 avankest 883: Modified</code> responses that are a result of a user agent generated
884: conditional request <em class="ct">must</em> be presented as <code>200
1.57 avankest 885: OK</code> responses with the appropriate content. The user agent
1.39 avankest 886: <em class="ct">must</em> allow scripts to override automatic cache
1.22 avankest 887: validation by setting request headers (e.g., <code>If-None-Match</code>,
1.42 avankest 888: <code>If-Modified-Since</code>), in which case <code>304 Not
889: Modified</code> responses <em class="ct">must</em> be passed through.
890: [<cite><span>RFC2616</span></cite>]</p>
1.12 avankest 891:
1.8 avankest 892: <p>If the user agent implements server-driven content-negotiation
1.42 avankest 893: it <em class="ct">should</em> set <code>Accept-Language</code>,
1.22 avankest 894: <code>Accept-Encoding</code> and <code>Accept-Charset</code> headers as
895: appropriate; it <em class="ct">must not</em> automatically set the
896: <code>Accept</code> header. Responses to such requests
1.40 avankest 897: <em class="ct">must</em> have the content-codings automatically
1.42 avankest 898: decoded. [<cite><span>RFC2616</span></cite>]</p>
1.8 avankest 899: </dd>
1.12 avankest 900:
1.14 avankest 901: <dt><dfn id="dfn-abort"><code>abort()</code></dfn>, method</dt>
1.12 avankest 902:
1.5 avankest 903: <dd>
1.60 avankest 904: <p>When invoked, the user agent <em class="ct">must</em> run the following
1.96 avankest 905: steps (unless otherwise noted):</p>
1.60 avankest 906:
907: <ol>
1.96 avankest 908: <li><p>Set the <span>response entity body</span> to "null" well as
909: removing any registered request headers.</p></li>
1.60 avankest 910:
911: <li><p>Any network activity for which the object responsible
912: <em class="ct">should</em> be cancelled.</p></li>
913:
1.83 avankest 914: <li>
1.85 avankest 915: <p>If the state is <span title="UNSENT state">UNSENT</span>,
1.83 avankest 916: <span title="OPEN state">OPEN</span> and the <span><code>send()</code>
1.87 avankest 917: flag</span> is "false", or <span title="DONE state">DONE</span> go to
918: the next step.</p>
1.85 avankest 919:
1.96 avankest 920: <p>Otherwise, switch the state to <span title="DONE state">DONE</span>
921: and synchronously dispatch a <code>readystatechange</code> event on the
922: object.</p>
1.83 avankest 923: </li>
1.67 avankest 924:
1.60 avankest 925: <li>
1.97 avankest 926: <p>Switch the state to <span title="UNSENT state">UNSENT</span>. (Do not
1.71 avankest 927: dispatch the <code>readystatechange</code> event.)</p>
1.60 avankest 928:
1.82 avankest 929: <p class="note">It is likely that a future version of the
930: <code>XMLHttpRequest</code> object will dispatch an
931: <code title="">abort</code> event here as well.</p>
1.60 avankest 932: </li>
933: </ol>
1.5 avankest 934: </dd>
1.12 avankest 935:
1.14 avankest 936: <dt><dfn id="dfn-getallresponseheaders"><code>getAllResponseHeaders()</code></dfn>,
1.22 avankest 937: method</dt>
1.12 avankest 938:
1.5 avankest 939: <dd>
1.60 avankest 940: <p>When invoked, the user agent <em class="ct">must</em> run
1.58 avankest 941: the following steps:</p>
942:
943: <ol>
1.86 avankest 944: <li><p>If the state is not <span title="LOADING state">LOADING</span> or
1.96 avankest 945: <span title="DONE state">DONE</span> raise an
1.98 avankest 946: <code>INVALID_STATE_ERR</code> exception and terminate these
947: steps.</p></li>
1.96 avankest 948:
949: <li><p>Return all the HTTP headers, as a single string, with each header
950: line separated by a U+000D CR U+000A LF pair excluding the status
951: line.</p></li>
1.58 avankest 952: </ol>
1.24 avankest 953:
1.5 avankest 954: <div class="example">
1.58 avankest 955: <pre><code>// The following script:
1.5 avankest 956: var client = new XMLHttpRequest();
1.14 avankest 957: client.open("GET", "test.txt", true);
1.5 avankest 958: client.send();
1.12 avankest 959: client.onreadystatechange = function() {
1.13 avankest 960: if(this.readyState == 3) {
1.12 avankest 961: print(this.getAllResponseHeaders());
962: }
963: }
1.5 avankest 964:
965: // ...should output something similar to the following text:
966: Date: 2004年10月24日 04:58:38 GMT
967: Server: Apache/1.3.31 (Unix)
968: Keep-Alive: timeout=15, max=99
969: Connection: Keep-Alive
970: Transfer-Encoding: chunked
1.58 avankest 971: Content-Type: text/plain; charset=utf-8</code></pre>
1.5 avankest 972: </div>
973: </dd>
1.12 avankest 974:
1.18 avankest 975: <dt><dfn id="dfn-getresponseheader" title="getresponseheader"><code>getResponseHeader(<var>header</var>)</code></dfn>,
1.22 avankest 976: method</dt>
1.12 avankest 977:
1.1 avankest 978: <dd>
1.58 avankest 979: <p>When the method is invoked, the user agent <em class="ct">must</em> run
980: the following steps:</p>
1.14 avankest 981:
1.58 avankest 982: <ol>
1.86 avankest 983: <li><p>If the state is not <span title="LOADING state">LOADING</span> or
1.96 avankest 984: <span title="DONE state">DONE</span> raise an
1.98 avankest 985: <code>INVALID_STATE_ERR</code> exception and terminate these
986: steps.</p></li>
1.58 avankest 987:
1.97 avankest 988: <li><p>If the <var>header</var> argument does not match the
1.96 avankest 989: <span><code>field-name</code> production</span> raise a
1.98 avankest 990: <code>SYNTAX_ERR</code> exception and terminate these steps.</p></li>
1.58 avankest 991:
1.80 avankest 992: <li><p>If the <var>header</var> argument
993: <span title="case-insensitive match">case-insensitively matches</span>
1.96 avankest 994: multiple HTTP headers for the last request sent return the values of
995: these headers as a single concatenated string separated from each other
1.98 avankest 996: by an U+002C COMMA followed by an U+0020 SPACE and terminate these
1.96 avankest 997: steps.</p></li>
1.58 avankest 998:
1.80 avankest 999: <li><p>If the <var>header</var> argument
1000: <span title="case-insensitive match">case-insensitively matches</span> a
1.96 avankest 1001: single HTTP header for the last request sent return the value of that
1.98 avankest 1002: header and terminate these steps.</p></li>
1.58 avankest 1003:
1.96 avankest 1004: <li><p>Return <code>null</code>.</p></li>
1.58 avankest 1005: </ol>
1.12 avankest 1006:
1.1 avankest 1007: <div class="example">
1.58 avankest 1008: <pre><code>// The following script:
1.1 avankest 1009: var client = new XMLHttpRequest();
1.14 avankest 1010: client.open("GET", "test.txt", true);
1.5 avankest 1011: client.send();
1.12 avankest 1012: client.onreadystatechange = function() {
1.13 avankest 1013: if(this.readyState == 3) {
1.14 avankest 1014: print(client.getResponseHeader("Content-Type"));
1.12 avankest 1015: }
1016: }
1.1 avankest 1017:
1.5 avankest 1018: // ...should output something similar to the following text:
1.58 avankest 1019: Content-Type: text/plain; charset=utf-8</code></pre>
1.1 avankest 1020: </div>
1.5 avankest 1021: </dd>
1.12 avankest 1022:
1.5 avankest 1023: <dt><dfn id="dfn-responsetext"><code>responseText</code></dfn> of type
1.22 avankest 1024: <code>DOMString</code>, readonly</dt>
1.12 avankest 1025:
1.5 avankest 1026: <dd>
1.88 avankest 1027: <p>On getting, the user agent <em class="ct">must</em> run the following
1028: steps:</p>
1029:
1030: <ol>
1031: <li><p>If the state is not <span title="LOADING state">LOADING</span> or
1.96 avankest 1032: <span title="DONE state">DONE</span> raise an
1.98 avankest 1033: <code>INVALID_STATE_ERR</code> exception and terminate these
1034: steps.</p></li>
1.12 avankest 1035:
1.88 avankest 1036: <li><p>Return the <span>text response entity body</span>.</p></li>
1037: </ol>
1.5 avankest 1038: </dd>
1.12 avankest 1039:
1.5 avankest 1040: <dt><dfn id="dfn-responsexml"><code>responseXML</code></dfn> of type
1.22 avankest 1041: <code>Document</code>, readonly</dt>
1.12 avankest 1042:
1.8 avankest 1043: <dd>
1.75 avankest 1044: <p>On getting, the user agent <em class="ct">must</em> run the following
1045: steps:</p>
1.79 avankest 1046:
1.75 avankest 1047: <ol>
1.96 avankest 1048: <li><p>If the state is not <span title="DONE state">DONE</span> raise an
1.98 avankest 1049: <code>INVALID_STATE_ERR</code> exception and terminate these
1050: steps.</p></li>
1.75 avankest 1051:
1.88 avankest 1052: <li><p>Return the <span>XML response entity body</span>.</p></li>
1.75 avankest 1053: </ol>
1.5 avankest 1054: </dd>
1.12 avankest 1055:
1.22 avankest 1056: <dt><dfn id="dfn-status"><code>status</code></dfn> of type <code>unsigned
1057: short</code>, readonly</dt>
1.12 avankest 1058:
1.5 avankest 1059: <dd>
1.72 avankest 1060: <p>On getting, if available, it <em class="ct">must</em> return the HTTP
1061: status code sent by the server (typically <code>200</code> for a
1062: successful request). Otherwise, if not available, the user agent
1063: <em class="ct">must</em> raise an <code>INVALID_STATE_ERR</code>
1064: exception.</p>
1.12 avankest 1065: </dd>
1066:
1.5 avankest 1067: <dt><dfn id="dfn-statustext"><code>statusText</code></dfn> of type
1.22 avankest 1068: <code>DOMString</code>, readonly</dt>
1.12 avankest 1069:
1.5 avankest 1070: <dd>
1.72 avankest 1071: <p>On getting, if available, it <em class="ct">must</em> return the HTTP
1072: status text sent by the server (appears after the status code). Otherwise,
1073: if not available, the user agent <em class="ct">must</em> raise an
1074: <code>INVALID_STATE_ERR</code> exception.</p>
1.1 avankest 1075: </dd>
1076: </dl>
1.12 avankest 1077:
1.31 avankest 1078: <h3 id="events">Events for the <code title="">XMLHttpRequest</code>
1079: Object</h3>
1.12 avankest 1080:
1.1 avankest 1081: <p>These sections describe the various events that can be dispatched on the
1.22 avankest 1082: object implementing the <code>XMLHttpRequest</code> interface. For this
1083: version of the specification only one event is defined.</p>
1.12 avankest 1084:
1.1 avankest 1085: <dl>
1086: <dt><dfn id="dfn-readystatechange"><code>readystatechange</code></dfn></dt>
1.24 avankest 1087:
1.72 avankest 1088: <dd>When the user agent dispatches a <code title="">readystatechange</code>
1089: event (as indicated above) it <em class="ct">must not</em> bubble,
1090: <em class="ct">must not</em> be cancelable and <em class="ct">must</em>
1091: implement the <code>Event</code> interface. Its <code>namespaceURI</code>
1092: attribute <em class="ct">must</em> be <code>null</code>.
1093: [<cite><span>DOM3Events</span></cite>]</dd>
1.1 avankest 1094: </dl>
1.32 avankest 1095:
1.31 avankest 1096: <h3 id="exceptions">Exceptions for the <code title="">XMLHttpRequest</code>
1097: Object</h3>
1098:
1.32 avankest 1099: <pre class="idl">exception <dfn>XMLHttpRequestException</dfn> {
1.31 avankest 1100: unsigned short code;
1101: };
1102: const unsigned short <span>NETWORK_ERR</span> = 101;</pre>
1103:
1.33 avankest 1104: <p>The <dfn id="network-err"><code>NETWORK_ERR</code></dfn> exception is
1105: thrown when a network error occurs in synchronous requests. See the section
1106: on <code>send()</code> for more details.</p>
1.11 avankest 1107:
1.29 avankest 1108: <h2 class="no-num" id="notcovered">Not in this Specification</h2>
1109:
1110: <p><em>This section is non normative.</em></p>
1111:
1.72 avankest 1112: <p>This specification does not include the following features which are
1113: being considered for a future version of this specification:</p>
1.29 avankest 1114:
1115: <ul>
1116: <li><code>load</code> event and <code>onload</code> attribute;</li>
1117: <li><code>error</code> event and <code>onerror</code> attribute;</li>
1118: <li><code>progress</code> event and <code>onprogress</code> attribute;</li>
1119: <li><code title="">abort</code> event and <code>onabort</code> attribute;</li>
1120: <li>Timers have been suggested, perhaps an <code>ontimeout</code>
1121: attribute;</li>
1122: <li>Property to disable following redirects;</li>
1.30 avankest 1123: <li><code title="">responseXML</code> for <code>text/html</code>
1124: documents;</li>
1.40 avankest 1125: <li>Cross-site <code title="">XMLHttpRequest</code>;</li>
1.87 avankest 1126: <li><code>responseBody</code> to deal with byte streams;</li>
1127: <li><code>getRequestHeader()</code> and
1128: <code>removeRequestHeader()</code>.</li>
1.29 avankest 1129: </ul>
1130:
1.1 avankest 1131: <h2 class="no-num" id="bibref">References</h2>
1.11 avankest 1132:
1.6 avankest 1133: <dl>
1.37 avankest 1134: <dt>[<dfn id="DOM3">DOM3Core</dfn>]</dt>
1.24 avankest 1135:
1.11 avankest 1136: <dd><cite><a href="http://www.w3.org/TR/DOM-Level-3-Core">Document Object
1.92 avankest 1137: Model (DOM) Level 3 Core Specification</a></cite>, A. Le Hors, P. Le
1138: Hégaret, L. Wood, G. Nicol, J. Robie, M. Champion, S. Byrne, editors. World
1139: Wide Web Consortium, April 2004.</dd>
1.11 avankest 1140:
1.37 avankest 1141: <dt>[<dfn id="DOM3EV">DOM3Events</dfn>]</dt>
1.24 avankest 1142:
1.92 avankest 1143: <dd><cite><a href="http://www.w3.org/TR/DOM-Level-3-Events/">Document
1144: Object Model (DOM) Level 3 Events Specification</a></cite>, Björn Höhrmann,
1145: editor. World Wide Web Consortium, April 2006.</dd>
1.11 avankest 1146:
1.37 avankest 1147: <dt>[<dfn id="ref-ecmascript">ECMAScript</dfn>]</dt>
1.24 avankest 1148:
1.92 avankest 1149: <dd><cite><a href="http://www.ecma-international.org/publications/standards/Ecma-262.htm">ECMAScript
1150: Language Specification</a></cite>, Third Edition. ECMA, December 1999.</dd>
1.14 avankest 1151:
1.37 avankest 1152: <dt>[<dfn id="ref-rfc2046">RFC2046</dfn>]</dt>
1.24 avankest 1153:
1.19 avankest 1154: <dd><cite><a href="http://ietf.org/rfc/rfc2046">Multipurpose Internet Mail
1.92 avankest 1155: Extensions (MIME) Part Two: Media Types</a></cite>, N. Freed, N.
1156: Borenstein, editors. IETF, November 1996.</dd>
1.14 avankest 1157:
1.37 avankest 1158: <dt>[<dfn id="RFC2119">RFC2119</dfn>]</dt>
1.24 avankest 1159:
1.92 avankest 1160: <dd><cite><a href="http://ietf.org/rfc/rfc2119">RFC 2119: Key words for use
1161: in RFCs to Indicate Requirement Levels</a></cite>, S. Bradner. IETF, March
1162: 1997.</dd>
1.12 avankest 1163:
1.37 avankest 1164: <dt>[<dfn id="RFC2616">RFC2616</dfn>]</dt>
1.24 avankest 1165:
1.11 avankest 1166: <dd><cite><a href="http://ietf.org/rfc/rfc2616">Hypertext Transfer Protocol
1.92 avankest 1167: -- HTTP/1.1</a></cite>, R. Fielding, J. Gettys, J. Mogul, H. Frystyk, L.
1168: Masinter, P. Leach, T. Berners-Lee, editors. IETF, June 1999.</dd>
1.12 avankest 1169:
1.37 avankest 1170: <dt>[<dfn id="ref-rfc2617">RFC2617</dfn>]</dt>
1.24 avankest 1171:
1.11 avankest 1172: <dd><cite><a href="http://ietf.org/rfc/rfc2617">HTTP Authentication: Basic
1.92 avankest 1173: and Digest Access Authentication</a></cite>, P. Hallam-Baker, J.
1174: Hostetler, S. Lawrence, P. Leach, A. Luotonen, L. Stewart, editors. IETF,
1175: June 1999.</dd>
1.12 avankest 1176:
1.37 avankest 1177: <dt>[<dfn id="ref-rfc2965">RFC2965</dfn>]</dt>
1.24 avankest 1178:
1.19 avankest 1179: <dd><cite><a href="http://ietf.org/rfc/rfc2965">HTTP State Management
1.92 avankest 1180: Mechanism</a></cite>, D. Kristol, L. Montulli, editors. IETF, October
1181: 2000.</dd>
1.19 avankest 1182:
1.37 avankest 1183: <dt>[<dfn id="ref-rfc3986">RFC3986</dfn>]</dt>
1.24 avankest 1184:
1.11 avankest 1185: <dd><cite><a href="http://ietf.org/rfc/rfc3986">Uniform Resource Identifier
1.92 avankest 1186: (URI): Generic Syntax</a></cite>, T. Berners-Lee, R. Fielding, L. Masinter,
1187: editors. IETF, January 2005.</dd>
1.28 avankest 1188:
1.92 avankest 1189: <dt>[<dfn id="ref-window">Window</dfn>]</dt>
1.28 avankest 1190:
1.92 avankest 1191: <dd><cite><a href="http://www.w3.org/TR/Window/">Window Object
1192: 1.0</a></cite>, I. Davis, M. Stachowiak, editors. W3C, April 2006.</dd>
1.41 avankest 1193:
1.92 avankest 1194: <dt>[<dfn id="ref-xml">XML</dfn>]</dt>
1.41 avankest 1195:
1.92 avankest 1196: <dd><cite><a href="http://www.w3.org/TR/xml/">Extensible Markup Language
1197: (XML) 1.0 (Fourth Edition)</a></cite>, T. Bray, J. Paoli, C.
1198: Sperberg-McQueen, E. Maler, F. Yergeau. W3C, September 2006.</dd>
1.41 avankest 1199:
1.92 avankest 1200: <dt>[<dfn id="ref-xmlns">XMLNS</dfn>]</dt>
1.41 avankest 1201:
1.92 avankest 1202: <dd><cite><a href="http://www.w3.org/TR/xml-names/">Namespaces in XML
1203: (Second Edition)</a></cite>, T. Bray, D. Hollander, A. Layman, R. Tobin.
1204: W3C, August 2006.</dd>
1.1 avankest 1205: </dl>
1.11 avankest 1206:
1.1 avankest 1207: <h2 class="no-num" id="acknowledgements">Acknowledgements</h2>
1.11 avankest 1208:
1.1 avankest 1209: <p><em>This section is non-normative</em></p>
1.12 avankest 1210:
1.7 avankest 1211: <p>The editor would like to thank to the following people who have contributed
1.33 avankest 1212: to this specification (ordered by first name):</p>
1.12 avankest 1213:
1.1 avankest 1214: <ul>
1.22 avankest 1215: <li>Alex Hopmann</li>
1.7 avankest 1216: <li>Alex Vincent</li>
1.15 avankest 1217: <li>Alexey Proskuryakov</li>
1.1 avankest 1218: <li>Asbjørn Ulsberg</li>
1219: <li>Boris Zbarsky</li>
1220: <li>Björn Höhrmann</li>
1221: <li>Cameron McCormack</li>
1222: <li>Christophe Jolif</li>
1223: <li>Charles McCathieNevile</li>
1.54 avankest 1224: <li>Dan Winship</li>
1.72 avankest 1225: <li>David Håsäther</li>
1.1 avankest 1226: <li>Dean Jackson</li>
1227: <li>Doug Schepers</li>
1228: <li>Douglas Livingstone</li>
1229: <li>Gorm Haug Eriksen</li>
1230: <li>Hallvord R. M. Steen</li>
1231: <li>Håkon Wium Lie</li>
1232: <li>Ian Davis</li>
1233: <li>Ian Hickson</li>
1234: <li>Ivan Herman</li>
1235: <li>Jens Lindström</li>
1236: <li>Jim Deegan</li>
1237: <li>Jim Ley</li>
1238: <li>Jonas Sicking</li>
1239: <li>Julian Reschke</li>
1240: <li>Karl Dubost</li>
1241: <li>Maciej Stachowiak</li>
1.7 avankest 1242: <li>Magnus Kristiansen</li>
1.1 avankest 1243: <li>Marc Hadley</li>
1.53 avankest 1244: <li>Marcos Caceres</li>
1.87 avankest 1245: <li>Mark Baker</li>
1.1 avankest 1246: <li>Mark Nottingham</li>
1.92 avankest 1247: <li>Mohamed Zergaoui</li>
1.1 avankest 1248: <li>Pawel Glowacki</li>
1249: <li>Robin Berjon</li>
1250: <li>Ruud Steltenpool</li>
1.33 avankest 1251: <li>Simon Pieters</li>
1.1 avankest 1252: </ul>
1.12 avankest 1253:
1.1 avankest 1254: <p>Special thanks to the Microsoft employees who first implemented the
1.22 avankest 1255: <code>XMLHttpRequest</code> interface, which was first widely deployed by the
1256: Windows Internet Explorer browser.</p>
1.12 avankest 1257:
1.54 avankest 1258: <p>Special thanks also to the WHATWG for drafting an initial version of this
1.22 avankest 1259: specification in their Web Applications 1.0 document.</p>
1.12 avankest 1260:
1.1 avankest 1261: <p>Thanks also to all those who have helped to improve this specification by
1.22 avankest 1262: sending suggestions and corrections. (Please, keep bugging us with your
1263: issues!)</p>
1.1 avankest 1264: </body>
1265: </html>
1.92 avankest 1266:
Webmaster