Annotation of 2006/webapi/XMLHttpRequest/Overview.src.html, revision 1.187
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">
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.89 avankest 12:  .issue { padding:.5em; border:solid #f00 }
1.16 avankest 13:  p.issue::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.122 avankest 21:  h1 code, h2 code, h3 code { color:inherit; background:inherit; font:inherit }
1.16 avankest 22:  </style>
1.147 avankest 23:  <link rel="stylesheet" href="http://www.w3.org/StyleSheets/TR/W3C-[STATUS]">
1.1 avankest 24: </head>
 25: <body>
 26:  <div class="head">
 27:  <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 28: 
1.156 avankest 29:  <h1 class="head" id="the-xmlhttprequest-object">The
 30:  <code title="">XMLHttpRequest</code> Object</h1>
1.12 avankest 31: 
1.149 avankest 32:  <h2 class="no-num no-toc" id="w3c-doctype">[LONGSTATUS] [DATE: 3 August 2002]</h2>
1.12 avankest 33: 
1.1 avankest 34:  <dl>
1.10 avankest 35:   <dt>This Version:</dt>
1.173 avankest 36:   <dd><a href="[VERSION]/">http://www.w3.org/TR/[YEAR]/ED-XMLHttpRequest-[CDATE]/</a></dd>
1.14 avankest 37: 
1.10 avankest 38:   <dt>Latest Version:</dt>
1.1 avankest 39:   <dd><a href="http://www.w3.org/TR/XMLHttpRequest/">http://www.w3.org/TR/XMLHttpRequest/</a></dd>
1.14 avankest 40: 
1.10 avankest 41:   <dt>Previous Versions:</dt>
1.173 avankest 42:   <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 43:   <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 44:   <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 45:   <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 46:   <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 47:   <dd><a href="http://www.w3.org/TR/2006/WD-XMLHttpRequest-20060619/">http://www.w3.org/TR/2006/WD-XMLHttpRequest-20060619/</a></dd>
 48:   <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 49: 
1.1 avankest 50:   <dt>Editor:</dt>
 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>
1.117 avankest 84:  at http://www.w3.org/TR/.</em></p>
1.36 avankest 85: 
1.172 avankest 86:  <p>This is the [DATE: 3 August 2002] Last Call Working Draft of The
1.147 avankest 87:  <code title="">XMLHttpRequest</code> Object specification. Please send
 88:  comments to <a href="mailto:public-webapi@w3.org">public-webapi@w3.org</a>
1.47 avankest 89:  (<a href="http://lists.w3.org/Archives/Public/public-webapi/">archived</a>)
1.172 avankest 90:  with either <samp>[XHR]</samp> or <samp title="">[XMLHttpRequest]</samp>
 91:  at the start of the subject line before befor 2 June 2008.</p>
1.51 avankest 92: 
1.47 avankest 93:  <p>This document is produced by the
 94:  <a href="http://www.w3.org/2006/webapi/">Web API Working Group</a>, part of
 95:  the <a href="http://www.w3.org/2006/rwc/Activity">Rich Web Clients
 96:  Activity</a> in the W3C <a href="http://www.w3.org/Interaction/">Interaction
1.51 avankest 97:  Domain</a>. Changes made to this document can be found in the
1.47 avankest 98:  <a href="http://dev.w3.org/cvsweb/2006/webapi/XMLHttpRequest/Overview.html">W3C
1.117 avankest 99:  public CVS server</a>.</p>
1.12 avankest 100: 
1.1 avankest 101:  <p>Publication as a Working Draft does not imply endorsement by the W3C
1.36 avankest 102:  Membership. This is a draft document and may be updated, replaced or
 103:  obsoleted by other documents at any time. It is inappropriate to cite this
 104:  document as other than work in progress.</p>
1.12 avankest 105: 
1.1 avankest 106:  <p>This document was produced by a group operating under the
1.36 avankest 107:  <a href="http://www.w3.org/Consortium/Patent-Policy-20040205/">5 February
1.52 avankest 108:  2004 W3C Patent Policy</a>. W3C maintains a
1.36 avankest 109:  <a rel="disclosure" href="http://www.w3.org/2004/01/pp-impl/38482/status">public
 110:  list of any patent disclosures</a> made in connection with the deliverables
 111:  of the group; that page also includes instructions for disclosing a patent.
 112:  An individual who has actual knowledge of a patent which the individual
 113:  believes contains
 114:  <a href="http://www.w3.org/Consortium/Patent-Policy-20040205/#def-essential">Essential
 115:  Claim(s)</a> must disclose the information in accordance with
 116:  <a href="http://www.w3.org/Consortium/Patent-Policy-20040205/#sec-Disclosure">section
 117:  6 of the W3C Patent Policy</a>.</p>
1.12 avankest 118: 
1.1 avankest 119:  <h2 class="no-num no-toc" id="toc">Table of Contents</h2>
1.21 avankest 120: 
1.1 avankest 121:  <!--toc-->
1.12 avankest 122: 
1.1 avankest 123:  <h2 id="introduction">Introduction</h2>
1.12 avankest 124: 
1.1 avankest 125:  <p><em>This section is non-normative.</em></p>
1.12 avankest 126: 
1.183 avankest 127:  <p>The <code>XMLHttpRequest</code> object implements an interface exposed
 128:  by a scripting engine that allows scripts to perform HTTP client
 129:  functionality, such as submitting form data or loading data from a
 130:  server. It is the ECMAScript HTTP API.</p>
1.24 avankest 131: 
1.1 avankest 132:  <p>The name of the object is <code>XMLHttpRequest</code> for compatibility
1.183 avankest 133:  with the Web, though each component of this name is potentially
 134:  misleading. First, the object supports any text based format, including
 135:  XML. Second, it can be used to make requests over both HTTP and HTTPS
 136:  (some implementations support protocols in addition to HTTP and HTTPS, but
 137:  that functionality is not covered by this specification). Finally, it
 138:  supports "requests" in a broad sense of the term as it pertains to HTTP;
 139:  namely all activity involved with HTTP requests or responses for the
 140:  defined HTTP methods.</p>
1.12 avankest 141: 
1.14 avankest 142:  <div class="example">
 143:  <p>Some simple code to do something with data from an XML document fetched
1.22 avankest 144:  over the network:</p>
1.16 avankest 145: 
1.58 avankest 146:  <pre><code>function test(data) {
1.14 avankest 147: // taking care of data
 148: }
 149: 
 150: function handler() {
1.117 avankest 151: if(this.readyState == 4 && this.status == 200) {
1.14 avankest 152:  // so far so good
1.117 avankest 153:  if(this.responseXML != null && this.responseXML.getElementById('test').firstChild.data)
 154:   // success!
1.14 avankest 155:  test(this.responseXML.getElementById('test').firstChild.data);
 156:  else
 157:  test(null);
1.117 avankest 158: } else if (this.readyState == 4 && this.status != 200) {
1.14 avankest 159:  // fetched the wrong page or network error...
 160:  test(null);
 161: }
 162: }
1.12 avankest 163: 
1.14 avankest 164: var client = new XMLHttpRequest();
 165: client.onreadystatechange = handler;
 166: client.open("GET", "test.xml");
1.58 avankest 167: client.send();</code></pre>
1.16 avankest 168: 
1.56 avankest 169:  <p>If you just want to log a message to the server:</p>
1.16 avankest 170: 
1.58 avankest 171:  <pre><code>function log(message) {
1.14 avankest 172: var client = new XMLHttpRequest();
1.56 avankest 173: client.open("POST", "/log");
1.57 avankest 174: client.setRequestHeader("Content-Type", "text/plain;charset=UTF-8");
1.14 avankest 175: client.send(message);
1.58 avankest 176: }</code></pre>
1.16 avankest 177: 
1.14 avankest 178:  <p>Or if you want to check the status of a document on the server:</p>
1.16 avankest 179: 
1.58 avankest 180:  <pre><code>function fetchStatus(address) {
1.14 avankest 181: var client = new XMLHttpRequest();
 182: client.onreadystatechange = function() {
 183:  // in case of network errors this might not give reliable results
 184:  if(this.readyState == 4)
 185:  returnStatus(this.status);
 186: }
 187: client.open("HEAD", address);
 188: client.send();
1.58 avankest 189: }</code></pre>
1.14 avankest 190:  </div>
1.12 avankest 191: 
1.153 avankest 192:  <h2 id="conformance">Conformance</h2>
1.12 avankest 193: 
1.27 avankest 194:  <p>Everything in this specification is normative except for diagrams,
 195:  examples, notes and sections marked non-normative.</p>
1.12 avankest 196: 
1.1 avankest 197:  <p>The key words <em class="ct">must</em>, <em class="ct">must not</em>,
1.74 avankest 198:  <em class="ct">should</em> and <em class="ct">may</em> in this document are
 199:  to be interpreted as described in RFC 2119.
 200:  [<cite><span>RFC2119</span></cite>]</p>
1.12 avankest 201: 
1.1 avankest 202:  <p>This specification defines the following classes of products:</p>
1.12 avankest 203: 
1.1 avankest 204:  <dl>
1.74 avankest 205:  <dt><dfn id="conforming-user-agent">Conforming user agent</dfn></dt>
1.24 avankest 206: 
1.74 avankest 207:  <dd>
 208:   <p>A user agent <em class="ct">must</em> behave as described in this
1.106 avankest 209:   specification in order to be considered conformant.</p>
1.95 avankest 210: 
1.140 avankest 211:   <p>If the user agent is not a conforming XML user agent the <span>XML
 212:   response entity body</span> <em class="ct">must</em> (always) be
 213:   <code>null</code>.</p>
 214: 
 215:   <p>User agents <em class="ct">may</em> implement algorithms given in
 216:   this specification in any way desired, so long as the end result is
 217:   indistinguishable from the result that would be obtained by the
 218:   specification's algorithms.</p>
1.110 avankest 219: 
1.95 avankest 220:   <p class="note">This specification uses both the terms "conforming user
 221:   agent(s)" and "user agent(s)" to refer to this product class.</p>
1.74 avankest 222:  </dd>
1.110 avankest 223: 
1.94 avankest 224:  <dt><dfn id="conforming-xml-user-agent">Conforming XML user agent</dfn></dt>
1.110 avankest 225: 
1.163 avankest 226:  <dd><p>An XML user agent <em class="ct">must</em> be a <span>conforming
 227:  user agent</span> and <em class="ct">must</em> be a conforming XML
 228:  processor that reports violations of namespace well-formedness.
 229:  [<cite><span>XML</span></cite>]
1.140 avankest 230:  [<cite><span>XMLNS</span></cite>]</p></dd>
1.1 avankest 231:  </dl>
1.32 avankest 232: 
1.153 avankest 233:  <h3 id="dependencies">Dependencies</h3>
1.33 avankest 234: 
1.29 avankest 235:  <p>This specification relies on several underlying specifications.</p>
 236:  <dl>
 237:  <dt>DOM</dt>
1.33 avankest 238: 
1.155 avankest 239:  <dd><p>A <span title="conforming user agent">conforming user agent</span>
1.176 avankest 240:  <em class="ct">must</em> support at least the subset of the functionality
1.182 avankest 241:  defined in DOM Events and DOM Core that this specification relies upon,
 242:  such as various exceptions and <code>EventTarget</code>.
1.155 avankest 243:  [<cite><span>DOM2Events</span></cite>]
 244:  [<cite><span>DOM3Core</span></cite>]</p></dd>
1.29 avankest 245: 
1.161 avankest 246:  <dt>HTML 5</dt>
 247: 
 248:  <dd>
1.182 avankest 249:   <p>A <span>conforming user agent</span> <em class="ct">must</em> at
 250:   least the subset of the functionality defined in HTML 5 that this
 251:   specification relies upon, such as the <code>Window</code> object and
 252:   serializing a <code>Document</code> object.</p>
1.161 avankest 253:   [<cite><span>HTML5</span></cite>]</p>
 254: 
 255:   <p class="note">The
 256:   <a href="http://www.w3.org/TR/2006/WD-Window-20060407/">Window Object
 257:   1.0</a> draft is not referenced normatively as it appears to be no
 258:   longer maintained and HTML 5 defines the <code>Window</code> object
 259:   in more detail. This specification already depends on HTML 5 for
 260:   other reasons so there is not much additional overhead because of
 261:   this.</p>
 262: 
1.29 avankest 263:  <dt>HTTP</dt>
1.33 avankest 264: 
1.29 avankest 265:  <dd>
1.155 avankest 266:   <p>A <span>conforming user agent</span>
1.126 avankest 267:   <em class="ct">must</em> support some version of the HTTP protocol. It
 268:   <em class="ct">should</em> support any HTTP method that matches the
1.149 avankest 269:   <span><code>Method</code> production</span> and <em class="ct">must</em>
 270:   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>
1.135 avankest 280: 
1.58 avankest 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.181 avankest 284:  
 285:  <dt>Web IDL</dt>
 286:  
 287:  <dd>A <span>conforming user agent</span> <em class="ct">must</em> also be
 288:  a conforming implementation of the IDL fragment in this specification, as
 289:  described in the Web IDL specification.
 290:  [<cite><span>WebIDL</span></cite>]
1.29 avankest 291:  </dl>
1.12 avankest 292: 
1.157 avankest 293: 
1.153 avankest 294:  <h3 id="terminology">Terminology</h3>
1.12 avankest 295: 
1.80 avankest 296:  <p>There is a <dfn id="case-insensitive-match">case-insensitive match</dfn>
1.153 avankest 297:  of strings <var>s1</var> and <var>s2</var> if after mapping the ASCII
 298:  character range A-Z to the range a-z both strings are identical.</p>
1.144 avankest 299: 
1.184 avankest 300:  <p>The terms <dfn id="origin">origin</dfn>,
 301:  <dfn id="same-origin">same origin</dfn>, and
1.157 avankest 302:  <dfn id="event-handler-attribute">event handler DOM attribute</dfn> are
 303:  defined by the HTML 5 specification.
 304:  [<cite><span>HTML5</span></cite>]</p>
1.155 avankest 305: 
1.80 avankest 306: 
1.153 avankest 307:  <h3 id="extensibility">Extensibility</h3>
1.12 avankest 308: 
1.81 avankest 309:  <p>Extensions of the API defined by this specification are <em>strongly
1.22 avankest 310:  discouraged</em>. User agents, Working Groups and other interested parties
1.33 avankest 311:  should discuss extensions on a relevant public forum, preferably
1.22 avankest 312:  <a href="mailto:public-webapi@w3.org">public-webapi@w3.org</a>.</p>
1.12 avankest 313: 
1.153 avankest 314: 
1.157 avankest 315: 
1.153 avankest 316:  <h2 id="security">Security Considerations</h2>
 317:  
1.154 avankest 318:  <p>Apart from requirements affecting security made throughout this
 319:  specification implementations <em class="ct">may</em>, at their
1.175 avankest 320:  discretion, not expose certain headers, such as headers containing
1.177 avankest 321:  HttpOnly cookies. [<cite><span>HTTPONLY</span></cite>]</p>
1.153 avankest 322: 
 323: 
1.157 avankest 324: 
1.153 avankest 325:  <h2 id="xmlhttprequest">The <code title="">XMLHttpRequest</code>
 326:  Object</h2>
1.12 avankest 327: 
1.58 avankest 328:  <p>The <code>XMLHttpRequest</code> object can be used by scripts to
1.22 avankest 329:  programmatically connect to their originating server via HTTP.</p>
1.12 avankest 330: 
1.1 avankest 331:  <p>Objects implementing the <code>XMLHttpRequest</code> interface
1.22 avankest 332:  <em class="ct">must</em> also implement the <code>EventTarget</code>
1.155 avankest 333:  interface. [<cite><span>DOM2Events</span></cite>]</p>
1.12 avankest 334: 
1.58 avankest 335:  <p>Objects implementing the <code title="">Window</code> interface
 336:  <em class="ct">must</em> provide an <code title="">XMLHttpRequest()</code>
1.155 avankest 337:  constructor. [<cite><span>HTML5</span></cite>]</p>
1.12 avankest 338: 
1.1 avankest 339:  <div class="example">
1.58 avankest 340:  <p>In ECMAScript this can be used as follows:</p>
 341: 
 342:  <pre><code>var client = new XMLHttpRequest();</code></pre>
1.1 avankest 343:  </div>
1.12 avankest 344: 
1.150 avankest 345:  <p>When the <code title="">XMLHttpRequest()</code> constructor is invoked
1.157 avankest 346:  a persistent pointer to the associated <code title="">Document</code>
 347:  object is stored on the newly created object. This is the
 348:  <dfn title="Document pointer" id="document-pointer"><code>Document</code>
 349:  pointer</dfn>. The associated <code>Document</code> object is the one
 350:  returned by the <code>document</code> attribute from the object on which
 351:  the <code title="">XMLHttpRequest()</code> constructor was invoked (a
 352:  <code>Window</code> object). The pointer can become "null" if the
 353:  object is destroyed.</p>
 354:  
 355:  <p class="note">As per the conformance criteria implementations are free
 356:  to implement this in any way they desire as long as the end results are
 357:  identical to those given by the English prose.</p>
1.27 avankest 358: 
1.58 avankest 359:  <div class="example">
1.157 avankest 360:  <p>If <var><code>iframe</code></var> is a <code title="">Window</code>
1.175 avankest 361:  object, <var><code>client</code></var> will have a pointer to
1.157 avankest 362:  <var><code>iframe.document</code></var> in the following example:</p>
1.58 avankest 363: 
1.157 avankest 364:  <pre><code>var client = new iframe.XMLHttpRequest()</code></pre>
1.58 avankest 365:  </div>
1.12 avankest 366: 
1.181 avankest 367:  <pre class="idl">[Constructor] interface <dfn id="xmlhttprequest-object">XMLHttpRequest</dfn> {
1.58 avankest 368:  // event handler
1.70 avankest 369:      attribute EventListener <span>onreadystatechange</span>;
1.58 avankest 370: 
 371:  // state
1.71 avankest 372:  const unsigned short <span title="UNSENT state">UNSENT</span> = 0;
1.134 avankest 373:  const unsigned short <span title="OPENED state">OPENED</span> = 1;
1.123 avankest 374:  const unsigned short <span title="HEADERS_RECEIVED state">HEADERS_RECEIVED</span> = 2;
1.86 avankest 375:  const unsigned short <span title="LOADING state">LOADING</span> = 3;
1.117 avankest 376:  const unsigned short <span title="DONE state">DONE</span> = 4;
1.70 avankest 377:  readonly attribute unsigned short <span>readyState</span>;
1.58 avankest 378: 
 379:  // request
1.70 avankest 380:  void <span>open</span>(in DOMString <var>method</var>, in DOMString <var>url</var>);
 381:  void <span>open</span>(in DOMString <var>method</var>, in DOMString <var>url</var>, in boolean <var>async</var>);
1.181 avankest 382:  void <span>open</span>(in DOMString <var>method</var>, in DOMString <var>url</var>, in boolean <var>async</var>, [Null=Null, Undefined=Null] in DOMString <var>user</var>);
 383:  void <span>open</span>(in DOMString <var>method</var>, in DOMString <var>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 384:  void <span>setRequestHeader</span>(in DOMString <var>header</var>, in DOMString <var>value</var>);
 385:  void <span>send</span>();
1.181 avankest 386:  void <span>send</span>([Null=Null, Undefined=Null] in DOMString <var>data</var>);
1.70 avankest 387:  void <span>send</span>(in Document <var>data</var>);
 388:  void <span>abort</span>();
1.58 avankest 389: 
 390:  // response
1.70 avankest 391:  DOMString <span>getAllResponseHeaders</span>();
 392:  DOMString <span>getResponseHeader</span>(in DOMString <var>header</var>);
 393:  readonly attribute DOMString <span>responseText</span>;
 394:  readonly attribute Document <span>responseXML</span>;
 395:  readonly attribute unsigned short <span>status</span>;
 396:  readonly attribute DOMString <span>statusText</span>;
1.1 avankest 397: };</pre>
1.8 avankest 398: 
1.58 avankest 399:  <p>The <code>XMLHttpRequest</code> object can be in five states:
1.71 avankest 400:  <span title="UNSENT state">UNSENT</span>,
1.134 avankest 401:  <span title="OPENED state">OPENED</span>,
1.123 avankest 402:  <span title="HEADERS_RECEIVED state">HEADERS_RECEIVED</span>,
1.86 avankest 403:  <span title="LOADING state">LOADING</span> and
1.79 avankest 404:  <span title="DONE state">DONE</span>. The current state is exposed through
 405:  the <code>readyState</code> attribute. The method definitions below define
 406:  when a state transition takes place.</p>
1.58 avankest 407: 
 408:  <p>When constructed, the <code>XMLHttpRequest</code> object
1.71 avankest 409:  <em class="ct">must</em> be in the UNSENT state. This state is represented
1.134 avankest 410:  by the <dfn id="unsent-state" title="UNSENT state"><code>UNSENT</code></dfn>
1.58 avankest 411:  constant, whose value is <code>0</code>.</p>
 412: 
1.134 avankest 413:  <p>The OPENED state is the state of the object when the <code>open()</code>
1.184 avankest 414:  method has been successfully invoked. During this state
 415:  <span>author request headers</span> can
1.58 avankest 416:  be set using <code>setRequestHeader()</code> and the request can be made
 417:  using <code>send()</code>. This state is represented by the
1.134 avankest 418:  <dfn id="opened-state" title="OPENED state"><code>OPENED</code></dfn> constant,
1.131 avankest 419:  whose value is <code>1</code>.</p>
1.58 avankest 420: 
1.134 avankest 421:  <p>The OPENED state has an associated <dfn id="send-flag"><code>send()</code>
1.185 avankest 422:  flag</dfn> that indicates whether the <code>send()</code> method has been
1.174 avankest 423:  invoked. It can be either "true" or "false" and has an initial value of
 424:  "false".</p>
1.87 avankest 425: 
1.123 avankest 426:  <p>The HEADERS_RECEIVED state is the state of the object when all response
 427:  headers have been received. This state is represented by the
1.131 avankest 428:  <dfn id="headers-received-state" title="HEADERS_RECEIVED state"><code>HEADERS_RECEIVED</code></dfn>
1.123 avankest 429:  constant, whose value is <code>2</code>.</p>
 430: 
 431:  <p>The LOADING state is the state of the object when the response entity body
 432:  is being received. This state is represented by the
1.131 avankest 433:  <dfn id="loading-state" title="LOADING state"><code>LOADING</code></dfn> constant,
1.86 avankest 434:  whose value is <code>3</code>.</p>
1.58 avankest 435: 
1.79 avankest 436:  <p>The DONE state is the state of the object when either the data transfer
1.58 avankest 437:  has been completed or something went wrong during the transfer (infinite
 438:  redirects for instance). This state is represented by the
1.131 avankest 439:  <dfn id="done-state" title="DONE state"><code>DONE</code></dfn> constant,
 440:  whose value is <code>4</code>.</p>
1.90 avankest 441: 
1.124 avankest 442:  <p>The DONE state has an associated <dfn id="error-flag">error flag</dfn>
1.185 avankest 443:  that indicates some type of network error or abortion. It can be either
1.174 avankest 444:  "true" or "false" and has an initial value of "false".</p>
1.124 avankest 445: 
1.88 avankest 446:  <p>The <dfn id="response-entity-body">response entity body</dfn> is the
 447:  fragment of the <span>entity body</span> received so far (LOADING state) or
 448:  the complete entity body (DONE state). If there is no entity body the
 449:  response entity body is "null".</p>
 450: 
 451:  <p>The <dfn id="text-response-entity-body">text response entity body</dfn>
1.169 avankest 452:  is a <code>DOMString</code> representing the <span>response entity
 453:  body</span>. The text response entity body is the return value of the
 454:  following algorithm:</p>
1.90 avankest 455: 
1.88 avankest 456:  <ol>
1.167 avankest 457:  <li><p>If the response entity body is "null" return the empty string and
1.107 avankest 458:  terminate these steps.</p>
1.90 avankest 459: 
1.113 avankest 460:  <li><p>Let <var>charset</var> be "null".</p></li>
1.90 avankest 461: 
 462:  <li><p>If there is no <code>Content-Type</code> header or there is a
 463:  <code>Content-Type</code> header which contains a MIME type that is
1.116 avankest 464:  <code>text/xml</code>, <code>application/xml</code> or ends in
1.167 avankest 465:  <code title="">+xml</code> (ignoring any parameters) use the rules set
 466:  forth in the XML specifications to determine the character encoding. Let
1.116 avankest 467:  <var>charset</var> be the determined character encoding.</p></li>
1.90 avankest 468: 
1.185 avankest 469:  <li><p>If the <code>Content-Type</code> header contains a
1.145 avankest 470:  <code>text/html</code> MIME type follow the rules set forth in the
 471:  HTML 5 specification to determine the character encoding. Let
 472:  <var>charset</var> be the determined character encoding.
 473:  [<cite><span>HTML5</span></cite>]</p></li>
1.118 avankest 474: 
1.115 avankest 475:  <li>
1.140 avankest 476:   <p>If the MIME type specified by the <code>Content-Type</code> header
 477:   contains a <code>charset</code> parameter and <var>charset</var> is
 478:   "null" let <var>charset</var> be the value of that parameter.</p>
1.118 avankest 479: 
1.145 avankest 480:   <p class="note">The algorithms described by the XML and HTML
 481:   specifications already take <code>Content-Type</code> into account.</p>
1.115 avankest 482:  </li>
1.118 avankest 483: 
1.111 avankest 484:  <li>
 485:   <!-- This stuff is copied from HTML5. Thanks Hixie! -->
1.113 avankest 486:   <p>If <var>charset</var> is "null" then, for each of the rows in the
1.127 avankest 487:   following table, starting with the first one and going down, if the first
 488:   bytes of <var>bytes</var> match the bytes given in the first column, then
 489:   let <var>charset</var> be the encoding given in the cell in the second
 490:   column of that row. If there is no match <var>charset</var> remains
 491:   "null".</p>
1.117 avankest 492: 
 493:   <table>
 494:   <thead>
 495:    <tr>
1.111 avankest 496:    <th>Bytes in Hexadecimal
 497:    <th>Description
1.117 avankest 498:   <tbody>
 499:    <tr>
1.111 avankest 500:    <td>00 00 FE FF
1.117 avankest 501:    <td>UTF-32BE BOM
 502:    <tr>
 503:    <td>FF FE 00 00
 504:    <td>UTF-32LE BOM
 505:    <tr>
 506:    <td>FE FF
 507:    <td>UTF-16BE BOM
 508:    <tr>
 509:    <td>FF FE
 510:    <td>UTF-16LE BOM
 511:    <tr>
 512:    <td>EF BB BF
 513:    <td>UTF-8 BOM<!-- nobody uses this
 514:    <tr>
 515:    <td>DD 73 66 73
 516:    <td>UTF-EBCDIC
 517: -->
 518:   </table>
1.111 avankest 519:  </li>
 520: 
1.113 avankest 521:  <li><p>If <var>charset</var> is "null" let <var>charset</var> be
 522:  UTF-8.</p></li>
 523: 
1.107 avankest 524:  <li><p>Return the result of decoding the response entity body using
1.167 avankest 525:  <var>charset</var>. Replace bytes or sequences of bytes that are not
1.173 avankest 526:  valid according to the <var>charset</var> with a single U+FFFD
1.167 avankest 527:  character.</p></li>
1.88 avankest 528:  </ol>
1.90 avankest 529: 
1.185 avankest 530:  <p class="note">Authors are encouraged to encode their resources using
 531:  UTF-8.</p>
1.163 avankest 532: 
1.88 avankest 533:  <p>The <dfn id="xml-response-entity-body">XML response entity body</dfn> is
1.107 avankest 534:  either a <code>Document</code> representing the <span>response entity
1.126 avankest 535:  body</span> or <code>null</code>. The XML response entity body is the return
 536:  value of the following algorithm:</p>
1.90 avankest 537: 
1.88 avankest 538:  <ol>
1.107 avankest 539:  <li><p>If the response entity body is "null" terminate these steps and
 540:  return <code>null</code>.</p></li>
1.88 avankest 541: 
1.103 avankest 542:  <li><p>If a <code>Content-Type</code> is present and it does not contain a
 543:  MIME type (ignoring any parameters) that is <code>text/xml</code>,
1.116 avankest 544:  <code>application/xml</code> or ends in <code title="">+xml</code> terminate
 545:  these steps and return <code>null</code>. (Do not terminate these steps if
 546:  there is no <code>Content-Type</code> header at all.)</p></li>
1.88 avankest 547: 
1.128 avankest 548:  <li>
 549:   <p>Parse the response entity body into a document tree following the
 550:   rules from the XML specifications. Let the result be <var>parsed
 551:   document</var>. If this fails (unsupported character encoding, namespace
 552:   well-formedness error et cetera) terminate these steps return
 553:   <code>null</code>. [<cite><span>XML</span></cite>]
 554:   [<cite><span>XMLNS</span></cite>]</p>
1.167 avankest 555: 
 556:   <p class="note">Scripts in the resulting document tree will not be
 557:   executed, resources referenced will not be loaded and no associated XSLT
 558:   will be applied.</p>
1.128 avankest 559:  </li>
1.88 avankest 560: 
1.96 avankest 561:  <li><p>Return an object implementing the <code>Document</code>
1.88 avankest 562:  interface representing the <var>parsed document</var>.</p></li>
 563:  </ol>
1.58 avankest 564: 
1.5 avankest 565:  <dl>
1.131 avankest 566:  <dt><dfn id="onreadystatechange"><code>onreadystatechange</code></dfn>
1.39 avankest 567:  of type <code>EventListener</code></dt>
1.12 avankest 568: 
1.157 avankest 569:  <dd><p>This attribute is an <span>event handler DOM attribute</span> and
 570:  <em class="ct">must</em> be invoked whenever a
1.173 avankest 571:  <code>readystatechange</code> event is targeted at the object.</p></dd>
1.12 avankest 572: 
1.131 avankest 573:  <dt><dfn id="readystate"><code>readyState</code></dfn> of type
1.22 avankest 574:  <code>unsigned short</code>, readonly</dt>
1.12 avankest 575: 
1.185 avankest 576:  <dd><p>On getting, the attribute <em class="ct">must</em> return the
 577:  value of the constant corresponding to the object's current
 578:  state.</p></dd>
1.12 avankest 579: 
1.131 avankest 580:  <dt><dfn id="open" title="open"><code>open(<var>method</var>,
1.22 avankest 581:  <var>url</var>, <var>async</var>, <var>user</var>,
 582:  <var>password</var>)</code></dfn>, method</dt>
1.12 avankest 583: 
1.1 avankest 584:  <dd>
1.58 avankest 585:   <p>When invoked, the user agent <em class="ct">must</em> follow the
1.96 avankest 586:   following steps (unless otherwise indicated):</p>
1.58 avankest 587: 
 588:   <ol>
1.156 avankest 589:   <li><p>Let <var>stored method</var> be the <var>method</var>
 590:   argument.</p></li>
1.58 avankest 591: 
1.156 avankest 592:   <li><p>If <var>stored method</var> does not match the
 593:   <dfn><code>Method</code> production</dfn>, defined in section 5.1.1 of
 594:   RFC 2616, raise a <code>SYNTAX_ERR</code> exception and terminate these
 595:   steps. [<cite><span>RFC2616</span></cite>]</p></li>
1.58 avankest 596: 
1.183 avankest 597:   <li>
 598:    <p>If <var>stored method</var>
 599:    <span title="case-insensitive match">case-insensitively matches</span>
 600:    <code>CONNECT</code>, <code>DELETE</code>, <code>GET</code>,
 601:    <code>HEAD</code>, <code>OPTIONS</code>, <code>POST</code>,
 602:    <code>PUT</code>, <code>TRACE</code>, or <code>TRACK</code> let
 603:    <var>stored method</var> be the canonical uppercase form of the
 604:    matched method name.</p>
 605:    
 606:    <p class="note">If it does not match any of the above, it is passed
 607:    through literally, including in the final request.</p>
 608:   </li>
1.156 avankest 609:   <!-- WebKit (and supposedly Firefox) also uppercase: COPY, INDEX, LOCK,
 610:   M-POST, MKCOL, MOVE, PROPFIND, PROPPATCH, and UNLOCK. -->
1.58 avankest 611: 
1.176 avankest 612:   <li>
 613:    <p>If <var>stored method</var> is one of <code>CONNECT</code>,
 614:    <code>TRACE</code>, or <code>TRACK</code> the user agent
 615:    <em class="ct">should</em> raise a <code>SECURITY_ERR</code> exception
 616:    and terminate these steps.</p>
 617:   
 618:    <p class="note"><code>TRACK</code> poses a security issue to legacy
 619:    server deployments.</p>
 620:   </li>
1.58 avankest 621: 
1.187 ! avankest 622:   <li><p>If <var>stored url</var> is a relative reference as defined by
 ! 623:   RFC 3987 resolve it using
1.96 avankest 624:   the current value of the <code>baseURI</code> attribute of the
1.157 avankest 625:   <span><code>Document</code> pointer</span>. If this fails raise a
1.187 ! avankest 626:   <code>SYNTAX_ERR</code> exception and terminate these steps.
 ! 627:   [<cite><span>RFC3987</span></cite>]</p></li>
 ! 628: 
 ! 629:   <li><p>Drop the fragment identifier (if any) from <var>url</var> and
 ! 630:   let <var>stored url</var> be the result of that operation.</p></li>
1.58 avankest 631: 
1.101 avankest 632:   <li><p>If <var>stored url</var> contains an unsupported scheme raise a
 633:   <code>NOT_SUPPORTED_ERR</code> and terminate these steps.</p></li>
1.110 avankest 634: 
1.58 avankest 635:   <li><p>If the <code>"user:password"</code> format in the
1.141 avankest 636:   <code>userinfo</code> production defined in section 3.2.1 of RFC 3986
 637:   is not supported for the relevant scheme and <var>stored url</var>
 638:   contains this format raise a <code>SYNTAX_ERR</code> and terminate
 639:   these steps. [<cite><span>RFC3986</span></cite>]</p></li>
1.58 avankest 640: 
 641:   <li><p>If <var>stored url</var> contains the <code>"user:password"</code>
 642:   format let <var>stored user</var> be the user part and <var>stored
 643:   password</var> be the password part.</p></li>
 644: 
 645:   <li><p>If <var>stored url</var> just contains the <code>"user"</code>
 646:   format let <var>stored user</var> be the user part.</p></li>
 647: 
1.184 avankest 648:   <li><p>If <var>stored url</var> is not of the <span>same origin</span>
1.157 avankest 649:   as the <span>origin</span> of the <span><code>Document</code>
 650:   pointer</span> the user agent <em class="ct">should</em> raise a
 651:   <code>SECURITY_ERR</code> exception and terminate these steps.</p></li>
1.58 avankest 652: 
1.145 avankest 653:   <li><p>Let <var>async</var> be the value of the <var>async</var>
 654:   argument or <code>true</code> if it was omitted.</p></li>
1.58 avankest 655: 
1.126 avankest 656:   <li><p>If the <var>user</var> argument was not omitted, and its syntax
1.145 avankest 657:   does not match that specified by the relevant authentication scheme,
 658:   raise a <code>SYNTAX_ERR</code> exception and terminate these
 659:   steps.</p></li>
1.14 avankest 660: 
1.58 avankest 661:   <li>
 662:    <p>If the <var>user</var> argument was not omitted and is not
1.145 avankest 663:    <code>null</code> let <var>stored user</var> be <var>user</var>
 664:    encoded using the encoding specified in the relevant authentication
 665:    scheme or UTF-8 if the scheme fails to specify an encoding.</p>
1.14 avankest 666: 
1.58 avankest 667:    <p class="note">This step overrides any user that may have been set by
 668:    the <var>url</var> argument.</p>
 669:   </li>
1.16 avankest 670: 
1.58 avankest 671:   <li><p>If the <var>user</var> argument was not omitted and is
 672:   <code>null</code> remove <var>stored user</var>.</p></li>
1.14 avankest 673: 
1.145 avankest 674:   <li><p>If the <var>password</var> argument was not omitted and its
 675:   syntax does not match that specified by the relevant authentication
 676:   scheme raise a <code>SYNTAX_ERR</code> exception and terminate these
 677:   steps.</p></li>
1.58 avankest 678: 
 679:   <li><p>If the <var>password</var> argument was not omitted and is not
 680:   <code>null</code> let <var>stored password</var> be <var>password</var>
 681:   encoded using the encoding specified in the relevant authentication
 682:   scheme or UTF-8 if the scheme fails to specify an encoding.</p></li>
 683: 
 684:   <li><p>If the <var>password</var> argument was not omitted and is
 685:   <code>null</code> remove <var>stored password</var>.</p></li>
 686: 
1.175 avankest 687:   <li>
 688:    <p><span title="abort send()">Abort the <code>send()</code>
 689:    algorithm</span>, set <span>response entity body</span> to "null" and
1.184 avankest 690:    empty the list of <span>author request headers</span>.<p>
1.175 avankest 691: 
 692:    <p class="note">This step and the next only has effect if
 693:    <code>send()</code> and/or <code>setRequestHeader()</code> has been
 694:    used.</p>
 695:   </li>
1.58 avankest 696: 
1.108 avankest 697:   <li><p>The user agent <em class="ct">should</em> cancel any network
 698:   activity for which the object is responsible.</p></li>
1.58 avankest 699:   <!-- we can hardly require it... -->
 700: 
1.145 avankest 701:   <li><p>Switch the object to the
 702:   <span title="OPENED state">OPENED</span> state, set the
 703:   <span><code>send()</code> flag</span> to "false" and then synchronously
 704:   dispatch a <code>readystatechange</code> event on the object and return
 705:   the method call.</p></li>
1.58 avankest 706:   </ol>
1.14 avankest 707: 
1.19 avankest 708:   <p class="note">A future version or extension of this specification will
1.33 avankest 709:   most likely define a way of doing cross-site requests.</p>
1.1 avankest 710:  </dd>
1.12 avankest 711: 
1.131 avankest 712:  <dt><dfn id="setrequestheader" title="setrequestheader"><code>setRequestHeader(<var>header</var>,
1.22 avankest 713:  <var>value</var>)</code></dfn>, method</dt>
1.12 avankest 714: 
1.5 avankest 715:  <dd>
1.184 avankest 716:   <p>
 717:   The object has an associated list of <dfn id="author-request-headers">author request headers</dfn>
 718:   consisting of HTTP header name/value pairs. The
 719:   <code>setRequestHeader()</code> method can be used to set new request
 720:   headers and append to request headers already in the list.</p>
 721:   
 722:   <p>As indicated in the algorithm below certain headers cannot be
 723:   set and are left up to the user agent. In addition there are certain
 724:   other headers the user agent will take control of if they are not set by
 725:   the author as indicated at the end of the <code>send()</code> method
 726:   section.</p>
1.163 avankest 727: 
 728:   <p class="note">The <code>setRequestHeader()</code> method appends a
 729:   value if the HTTP header given as argument is already part of the list
 730:   of request headers.</p>
1.45 avankest 731: 
1.58 avankest 732:   <p>When invoked, the user agent <em class="ct">must</em> follow the
1.96 avankest 733:   following steps (unless otherwise indicated):</p>
1.14 avankest 734: 
1.58 avankest 735:   <ol>
 736:   <li><p>If the state of the object is not
1.134 avankest 737:   <span title="OPENED state">OPENED</span> raise an
1.98 avankest 738:   <code>INVALID_STATE_ERR</code> exception and terminate these
 739:   steps.</p></li>
1.96 avankest 740: 
 741:   <li><p>If the <span><code>send()</code> flag</span> is "true" raise an
1.98 avankest 742:   <code>INVALID_STATE_ERR</code> exception and terminate these
 743:   steps.</p></li>
1.14 avankest 744: 
1.97 avankest 745:   <li><p>If the <var>header</var> argument does not match the
1.22 avankest 746:   <dfn><code>field-name</code> production</dfn> as defined by section 4.2
1.181 avankest 747:   of RFC 2616 raise a <code>SYNTAX_ERR</code> exception and terminate
 748:   these steps. [<cite><span>RFC2616</span></cite>]</p></li>
1.79 avankest 749: 
1.178 avankest 750:   <li>
 751:    <p>If the <var>value</var> argument does not match the
 752:    <dfn><code>field-value</code> production</dfn> as defined by section
 753:    4.2 of RFC 2616 raise a <code>SYNTAX_ERR</code> and terminate these
 754:    steps. [<cite><span>RFC2616</span></cite>]</p>
 755: 
1.184 avankest 756:    <p class="note">The empty string is legal and represents the empty
 757:    header value.</p>
1.178 avankest 758:   </li>
1.14 avankest 759: 
1.32 avankest 760:   <li>
1.132 avankest 761:    <p>For security reasons, these steps <em class="ct">should</em> be
1.98 avankest 762:    terminated if the <var>header</var> argument
1.80 avankest 763:    <span title="case-insensitive match">case-insensitively matches</span>
 764:    one of the following headers:</p>
1.58 avankest 765: 
1.32 avankest 766:    <ul>
 767:    <li><code>Accept-Charset</code></li>
 768:    <li><code>Accept-Encoding</code></li>
1.176 avankest 769:    <li><code>Authorization</code></li>
1.68 avankest 770:    <li><code>Connection</code></li>
1.32 avankest 771:    <li><code>Content-Length</code></li>
1.176 avankest 772:    <li><code>Cookie</code></li>
 773:    <li><code>Cookie2</code></li>
1.68 avankest 774:    <li><code>Content-Transfer-Encoding</code></li>
 775:    <li><code>Date</code></li>
1.32 avankest 776:    <li><code>Expect</code></li>
 777:    <li><code>Host</code></li>
 778:    <li><code>Keep-Alive</code></li>
 779:    <li><code>Referer</code></li>
 780:    <li><code>TE</code></li>
 781:    <li><code>Trailer</code></li>
 782:    <li><code>Transfer-Encoding</code></li>
 783:    <li><code>Upgrade</code></li>
1.184 avankest 784:    <li><code>User-Agent</code></li>
1.68 avankest 785:    <li><code>Via</code></li>
1.32 avankest 786:    </ul>
1.184 avankest 787:    
 788:    <p>… or if the start of the <var>header</var> argument
 789:    <span title="case-insensitive match">case-insensitively matches</span>
 790:    <code>Proxy-</code> or <code>Sec-</code> (including when the argument
 791:    is just <code>Proxy-</code> or <code>Sec-</code>).</p>
 792:    
 793:    <p class="note">The above headers are not allowed to be set as they
 794:    are better controlled by the user agent as it knows best what value
 795:    they should have. Header names starting with <code>Sec-</code> are not
 796:    allowed to be set to allow new headers to be minted in the future that
 797:    are guaranteed not to come from <code>XMLHttpRequest</code>. (Older
 798:    clients would however still be vulnerable as they allow such headers
 799:    to be set.)</p>
1.32 avankest 800:   </li>
1.5 avankest 801: 
1.184 avankest 802:   <li><p>If the <var>header</var> argument is not in the <span>author request
 803:   headers</span> list append the <var>header</var> with its associated <var>value</var>
1.98 avankest 804:   to the list and terminate these steps.</p></li>
1.58 avankest 805: 
1.184 avankest 806:   <li><p>If the <var>header</var> argument is in the <span>author request
 807:   headers</span> list either use multiple headers, combine the values or use a
1.96 avankest 808:   combination of those (section 4.2, RFC 2616).
 809:   [<cite><span>RFC2616</span></cite>]</p></li>
 810:   <!-- XXX it seems UAs always combine the values -->
1.58 avankest 811:   </ol>
1.45 avankest 812: 
 813:   <p class="note">See also the <code>send()</code> method regarding user
1.71 avankest 814:   agent header handling for caching, authentication, proxies, and
 815:   cookies.</p>
1.14 avankest 816: 
 817:   <div class="example">
1.58 avankest 818:   <pre><code>// The following script:
1.14 avankest 819: var client = new XMLHttpRequest();
 820: client.open('GET', 'demo.cgi');
 821: client.setRequestHeader('X-Test', 'one');
 822: client.setRequestHeader('X-Test', 'two');
 823: client.send();
 824: 
 825: // ...would result in the following header being sent:
 826: ...
 827: X-Test: one, two
1.58 avankest 828: ...</code></pre>
1.14 avankest 829:   </div>
1.5 avankest 830:  </dd>
1.12 avankest 831: 
1.131 avankest 832:  <dt><dfn id="send" title="send"><code>send(<var>data</var>)</code></dfn>,
1.22 avankest 833:  method</dt>
1.12 avankest 834: 
1.17 avankest 835:  <dd>
1.58 avankest 836:   <p>The <code>send()</code> method initiates the request and its optional
1.163 avankest 837:   argument provides the <span>entity body</span>.</p>
1.181 avankest 838: 
1.163 avankest 839:   <p class="note">Authors are encouraged to ensure that they have
 840:   specified the <code>Content-Type</code> header via
 841:   <code>setRequestHeader()</code> before invoking <code>send()</code> with
 842:   a non-<code>null</code> <var>data</var> argument.</p>
1.58 avankest 843: 
 844:   <p>When invoked, the user agent <em class="ct">must</em> follow the
1.108 avankest 845:   following steps (unless otherwise noted). Note that this algorithm might
1.126 avankest 846:   get aborted if the <code>open()</code> or <code>abort()</code> method is
 847:   invoked. When the
1.109 avankest 848:   <dfn title="abort send()" id="abort-send-algorithm"><code>send()</code>
1.110 avankest 849:   algorithm is aborted</dfn> the user agent <em class="ct">must</em>
 850:   terminate the algorithm after finishing the step it is on.</p>
 851: 
1.184 avankest 852:   <p class="note">The following algorithm cannot be aborted through
 853:   script when <var>async</var> is <code>false</code>. It can only be
 854:   aborted when <var>async</var> is <code>true</code> and only after the
 855:   method call has returned.</p>
1.58 avankest 856: 
 857:   <ol>
 858:   <li><p>If the state of the object is not
1.134 avankest 859:   <span title="OPENED state">OPENED</span> raise an
1.98 avankest 860:   <code>INVALID_STATE_ERR</code> exception and terminate these
 861:   steps.</p></li>
1.96 avankest 862: 
 863:   <li><p>If the <span><code>send()</code> flag</span> is "true" raise an
1.98 avankest 864:   <code>INVALID_STATE_ERR</code> exception and terminate these
 865:   steps.</p></li>
1.58 avankest 866: 
1.102 avankest 867:   <li><p>If <var>async</var> is <code>true</code> set the
1.103 avankest 868:   <span><code>send()</code> flag</span> to "true".</p></li>
 869: 
1.58 avankest 870:   <li>
1.183 avankest 871:    <p>If <var>stored method</var> is <code>GET</code> or
 872:    <code>HEAD</code> act as if the
1.166 avankest 873:    <var>data</var> argument is <code>null</code>.</p>
1.181 avankest 874: 
1.58 avankest 875:    <p>If the <var>data</var> argument has not been omitted and is not
1.145 avankest 876:    <code>null</code> use it for the <dfn id="entity-body">entity
 877:    body</dfn> as defined by section 7.2 of RFC 2616 observing the
 878:    following rules: [<cite><span>RFC2616</span></cite>]</p>
1.58 avankest 879: 
1.119 avankest 880:    <dl class="switch">
1.58 avankest 881:    <dt><var>data</var> is a <code>DOMString</code></dt>
 882: 
1.163 avankest 883:    <dd>
 884:     <p>Encode <var>data</var> using UTF-8 for transmission.</p>
 885: 
1.164 avankest 886:     <p>If a <code>Content-Type</code> header is set using
 887:     <code>setRequestHeader()</code> set the <code>charset</code>
 888:     parameter of that header to <code>UTF-8</code>.</p>
1.163 avankest 889:    </dd>
1.58 avankest 890: 
 891:    <dt><var>data</var> is a <code>Document</code>
 892: 
 893:    <dd>
1.181 avankest 894:     <p>Let <var>data</var> be <code><var>data</var>.innerHTML</code> as
1.182 avankest 895:     defined by section 2.5 of HTML 5. Encode it using
1.183 avankest 896:     <code><var>data</var>.inputEncoding</code> or UTF-8 if
 897:     <code><var>data</var>.inputEncoding</code> is
1.181 avankest 898:     <code>null</code>. Re-raise any exceptions the
 899:     <code><var>data</var>.innerHTML</code> getter algorithm raises.
 900:     [<cite><span>HTML5</span></cite>]</p>
1.183 avankest 901: 
1.181 avankest 902:     <p class="note">If the document cannot be serialized the
 903:     <code>document.innerHTML</code> algorithm raises an
 904:     <code>INVALID_STATE_ERR</code> exception.</p>
1.183 avankest 905: 
1.165 avankest 906:     <p>If no <code>Content-Type</code> header has been set using
1.175 avankest 907:     <code>setRequestHeader()</code> set a <code>Content-Type</code>
 908:     request header with a value of
1.165 avankest 909:     <code>application/xml;charset=<var>charset</var></code> where
 910:     <var>charset</var> is the encoding used to encode the document.</p>
1.58 avankest 911: 
 912:     <p class="note">Subsequent changes to the <code>Document</code> have
 913:     no effect on what is submitted.</p>
 914:    </dd>
 915: 
 916:    <dt><var>data</var> is not a <code>DOMString</code> or
 917:    <code>Document</code></dt>
 918: 
1.96 avankest 919:    <dd><p>Use the stringification mechanisms of the host language on
 920:    <var>data</var> and treat the result as if <var>data</var> is a
1.166 avankest 921:    <code>DOMString</code>. Or, if this fails, act as if the
 922:    <var>data</var> argument is <code>null</code>.</p></dd>
1.58 avankest 923:    </dl>
 924: 
1.126 avankest 925:    <p>If the <var>data</var> argument has been omitted, or is
 926:    <code>null</code>, no entity body is used in the request.</p>
1.58 avankest 927:   </li>
 928: 
1.71 avankest 929:   <li><p>Make a request to <var>stored url</var>, using HTTP method
 930:   <var>stored method</var>, user <var>stored user</var> (if provided) and
 931:   password <var>stored password</var> (if provided), taking into account
1.184 avankest 932:   the entity body, list of <span>author request headers</span> and the
 933:   rules listed directly after this set of steps.</p></li>
1.60 avankest 934: 
1.67 avankest 935:   <li>
1.96 avankest 936:    <p>Synchronously dispatch a <code>readystatechange</code> event on the
 937:    object.</p>
1.67 avankest 938: 
1.96 avankest 939:    <p class="note">The state of the object does not change. The event is
 940:    dispatched for historical reasons.</p>
1.67 avankest 941:   </li>
 942: 
1.102 avankest 943:   <li><p>If <var>async</var> is <code>true</code> return the
1.98 avankest 944:   <code>send()</code> method call. (Do not terminate the steps in the
 945:   algorithm though.)</p></li>
1.58 avankest 946: 
1.117 avankest 947:   <li>
1.175 avankest 948:    <p>While executing the request the following rules are to be
1.123 avankest 949:    observed.</p>
 950: 
1.119 avankest 951:    <dl class="switch">
1.118 avankest 952:    <dt>If the response is an HTTP redirect</dt>
1.167 avankest 953: 
1.118 avankest 954:    <dd>
1.145 avankest 955:     <p>If the redirect does not violate security (it is
1.184 avankest 956:     <span>same origin</span> for instance) or infinite loop precautions
1.145 avankest 957:     and the scheme is supported transparently follow the redirect and go
1.166 avankest 958:     to the start of this step (step 8).</p>
1.145 avankest 959: 
 960:     <p class="note">HTTP places requirements on the user agent regarding
 961:     the preservation of the request method and entity body during
 962:     redirects, and also requires users to be notified of certain kinds
 963:     of automatic redirections.</p>
1.118 avankest 964:     <!-- Arguably HTTP should be fixed for the latter case. No browser
 965:     follows that as far as I know. -->
 966: 
 967:     <p>Otherwise, follow the following set of steps:</p>
1.145 avankest 968: 
1.118 avankest 969:     <ol>
1.124 avankest 970:     <li><p>Set the <span>response entity body</span> to "null", the
1.184 avankest 971:     <span>error flag</span> to "true" and empty the list of
 972:     <span>author request headers</span>.</p></li>
1.118 avankest 973: 
 974:     <li><p>Synchronously switch the state to
 975:     <span title="DONE state">DONE</span>.</p></li>
 976: 
 977:     <li><p>If <var>async</var> is set to <code>false</code> raise a
 978:     <code>NETWORK_ERR</code> exception and terminate the overall
 979:     algorithm.</p></li>
 980: 
 981:     <li><p>Synchronously dispatch a <code>readystatechange</code> event on
 982:     the object.</p></li>
 983: 
 984:     <li><p>Terminate the overall algorithm.</p></li>
 985:     </ol>
 986: 
 987:     <p class="note">It is likely that a future version of the
 988:     <code>XMLHttpRequest</code> object will dispatch an <code>error</code>
 989:     event here as well.</p>
 990:    </dd>
1.117 avankest 991: 
1.121 avankest 992: 
1.118 avankest 993:    <dt>If the user cancels the download</dt>
1.58 avankest 994: 
1.118 avankest 995:    <dd>
 996:     <p>Run the following set of steps:</p>
1.60 avankest 997: 
1.118 avankest 998:     <ol>
1.124 avankest 999:     <li><p>Set the <span>response entity body</span> to "null", the
1.184 avankest 1000:     <span>error flag</span> to "true" and empty the list of
 1001:     <span>author request headers</span>.</p></li>
1.118 avankest 1002: 
 1003:     <li><p>Synchronously switch the state to
 1004:     <span title="DONE state">DONE</span>.</p></li>
 1005: 
1.121 avankest 1006:     <li><p>If <var>async</var> is set to <code>false</code> raise an
 1007:     <code>ABORT_ERR</code> exception and terminate the overall
1.118 avankest 1008:     algorithm.</p></li>
 1009: 
 1010:     <li><p>Synchronously dispatch a <code>readystatechange</code> event on
 1011:     the object.</p></li>
 1012: 
 1013:     <li><p>Terminate the overall algorithm.</p></li>
 1014:     </ol>
1.121 avankest 1015: 
1.118 avankest 1016:     <p class="note">It is likely that a future version of the
1.121 avankest 1017:     <code>XMLHttpRequest</code> object will dispatch an
 1018:     <code title="">abort</code> event here as well.</p>
1.118 avankest 1019:    </dd>
1.60 avankest 1020: 
1.121 avankest 1021: 
1.118 avankest 1022:    <dt>In case of network errors</dt>
1.60 avankest 1023: 
1.118 avankest 1024:    <dd>
1.176 avankest 1025:     <p>In case of DNS errors, timeout, TLS negotiation failure, or other
1.186 avankest 1026:     type of network errors, do not request user interaction and run the
 1027:     following set of steps:</p>
 1028: 
 1029:     <p class="note">This does not include HTTP responses that
 1030:     indicate some type of error, such as HTTP status code 410.</p>
1.118 avankest 1031: 
 1032:     <ol>
1.124 avankest 1033:     <li><p>Set the <span>response entity body</span> to "null", the
1.184 avankest 1034:     <span>error flag</span> to "true" and empty the list of
 1035:     <span>author request headers</span>.</p></li>
1.118 avankest 1036: 
 1037:     <li><p>Synchronously switch the state to
 1038:     <span title="DONE state">DONE</span>.</p></li>
 1039: 
 1040:     <li><p>If <var>async</var> is set to <code>false</code> raise a
 1041:     <code>NETWORK_ERR</code> exception and terminate the overall
 1042:     algorithm.</p></li>
 1043: 
 1044:     <li><p>Synchronously dispatch a <code>readystatechange</code> event on
 1045:     the object.</p></li>
 1046: 
 1047:     <li><p>Terminate the overall algorithm.</p></li>
 1048:     </ol>
 1049: 
 1050:     <p class="note">It is likely that a future version of the
 1051:     <code>XMLHttpRequest</code> object will dispatch an <code>error</code>
 1052:     event here as well.</p>
 1053:    </dd>
 1054:    
 1055:    <dt>Once all HTTP headers have been received</dt>
 1056:    
 1057:    <dd>
 1058:     <p>If all HTTP headers have been received, before receiving the message
1.123 avankest 1059:     body (if any), run the following steps:</p>
1.60 avankest 1060: 
1.118 avankest 1061:     <ol>
 1062:     <li><p>Synchronously switch the state to
1.123 avankest 1063:     <span title="HEADERS_RECEIVED state">HEADERS_RECEIVED</span>.</p></li>
1.118 avankest 1064: 
 1065:     <li><p>Synchronously dispatch a <code>readystatechange</code> event on
 1066:     the object.</p></li>
1.123 avankest 1067:     </ol>
 1068:    </dd>
 1069:    
 1070:    <dt>Once the first byte (or more) of the response entity body has been
 1071:    received</dt>
 1072:    <dt>If there is no response entity body</dt>
1.153 avankest 1073: 
1.123 avankest 1074:    <dd>
 1075:     <ol>
1.118 avankest 1076:     <li><p>Synchronously switch the state to
 1077:     <span title="LOADING state">LOADING</span>.</p></li>
 1078: 
 1079:     <li><p>Synchronously dispatch a <code>readystatechange</code> event on
 1080:     the object.</p></li>
 1081:     </ol>
 1082:    </dd>
 1083:    </dl>
1.153 avankest 1084: 
1.124 avankest 1085:    <p>Finally, once the complete resource has been downloaded go to the next
1.118 avankest 1086:    step.</p>
1.58 avankest 1087:   </li>
 1088: 
1.82 avankest 1089:   <li><p>When the request has successfully completed loading, synchronously
1.96 avankest 1090:   switch the state to <span title="DONE state">DONE</span> and then
 1091:   synchronously dispatch a <code>readystatechange</code> event on the
 1092:   object and return the method call in case of <var>async</var> being
 1093:   <code>false</code>.</p></li>
1.58 avankest 1094:   </ol>
1.14 avankest 1095: 
1.91 avankest 1096:   <p>If the user agent allows the user to configure a proxy it
1.22 avankest 1097:   <em class="ct">should</em> modify the request appropriately;
1.42 avankest 1098:   <abbr title="in other words">i.e.</abbr>, connect to the proxy host
 1099:   instead of the origin server, modify the <code>Request-Line</code> and
 1100:   send <code>Proxy-Authorization</code> headers as specified.</p>
1.12 avankest 1101: 
1.42 avankest 1102:   <p>If the user agent supports HTTP Authentication it
1.184 avankest 1103:   <em class="ct">should</em> consider requests originating from this
 1104:   object to be part of the protection space that includes the accessed
 1105:   URIs and send <code>Authorization</code> headers and handle <code>401
 1106:   Unauthorized</code> requests appropriately. If authentication fails,
1.185 avankest 1107:   and <var>stored user</var> and <var>stored password</var> are not
 1108:   provided, user agents <em class="ct">should</em> prompt the users for
 1109:   credentials.
1.42 avankest 1110:   [<cite><span>RFC2617</span></cite>]</p>
 1111: 
1.185 avankest 1112:   <p class="note">Users are not prompted if credentials are provided
 1113:   through the <code>open()</code> API so that authors can implement their
 1114:   own user interface.</p>
 1115: 
1.57 avankest 1116:   <p>If the user agent supports HTTP State Management it
1.42 avankest 1117:   <em class="ct">should</em> persist, discard and send cookies (as received
 1118:   in the <code>Set-Cookie</code> and <code>Set-Cookie2</code> response
 1119:   headers, and sent in the <code>Cookie</code> header) as applicable.
1.178 avankest 1120:   [<cite><span>RFC2109</span></cite>]
1.42 avankest 1121:   [<cite><span>RFC2965</span></cite>]</p>
 1122: 
1.178 avankest 1123:   <!-- These specs do not match reality. -->
1.163 avankest 1124: 
1.42 avankest 1125:   <p>If the user agent implements a HTTP cache it <em class="ct">should</em>
1.184 avankest 1126:   respect <code>Cache-Control</code> request headers set by
 1127:   <code>setRequestHeader()</code> (e.g., <code>Cache-Control: no-cache</code>
 1128:   bypasses the cache). It <em class="ct">must not</em> send
1.42 avankest 1129:   <code>Cache-Control</code> or <code>Pragma</code> request headers
1.137 avankest 1130:   automatically unless the user explicitly requests such behavior
1.179 avankest 1131:   (<abbr>e.g.</abbr>, by (force-)reloading the page).</p>
1.184 avankest 1132: 
1.179 avankest 1133:   <p>For <code>304 Not Modified</code> responses that are a result of a
 1134:   user agent generated conditional request the user agent
 1135:   <em class="ct">must</em> act as if the server gave a <code>200
 1136:   OK</code> response with the appropriate content. The user agent
1.184 avankest 1137:   <em class="ct">must</em> allow <code>setRequestHeader()</code> to
 1138:   override automatic cache validation by setting request headers (e.g.,
 1139:   <code>If-None-Match</code>, <code>If-Modified-Since</code>), in which
 1140:   case <code>304 Not Modified</code> responses <em class="ct">must</em> be
 1141:   passed through. [<cite><span>RFC2616</span></cite>]</p>
1.12 avankest 1142: 
1.8 avankest 1143:   <p>If the user agent implements server-driven content-negotiation
1.161 avankest 1144:   it <em class="ct">should</em> set <code>Accept-Encoding</code> and
1.174 avankest 1145:   <code>Accept-Charset</code> headers as appropriate.
 1146:   Unless set through <code>setRequestHeader()</code> user agents
 1147:   <em class="ct">should</em> set the <code>Accept</code> and
1.180 avankest 1148:   <code>Accept-Language</code> headers as well. If <code>Accept</code> is
 1149:   set by the user agent it <em class="ct">must</em> have the value
 1150:   <code>*/*</code>. Responses <em class="ct">must</em> have the
 1151:   content-encodings automatically decoded.
 1152:   [<cite><span>RFC2616</span></cite>]</p>
1.184 avankest 1153:   
1.185 avankest 1154:   <p>Besides the <span>author request headers</span> user agents
 1155:   <em class="ct">should not</em> include additional request headers other
 1156:   than those mentioned above or other than those authors are not allowed
 1157:   to set using <code>setRequestHeader()</code>. This ensures that authors
 1158:   have a reasonably predictable API.</p>
1.8 avankest 1159:  </dd>
1.12 avankest 1160: 
1.131 avankest 1161:  <dt><dfn id="abort"><code>abort()</code></dfn>, method</dt>
1.12 avankest 1162: 
1.5 avankest 1163:  <dd>
1.60 avankest 1164:   <p>When invoked, the user agent <em class="ct">must</em> run the following
1.96 avankest 1165:   steps (unless otherwise noted):</p>
1.60 avankest 1166: 
 1167:   <ol>
1.108 avankest 1168:   <li><p><span title="abort send()">Abort the <code>send()</code>
 1169:   algorithm</span>, set the <span>response entity body</span> to "null",
1.135 avankest 1170:   the <span>error flag</span> to "true" and remove any registered request
 1171:   headers.</p></li>
1.60 avankest 1172: 
1.108 avankest 1173:   <li><p>The user agent <em class="ct">should</em> cancel any network
 1174:   activity for which the object is responsible.</p></li>
1.60 avankest 1175: 
1.83 avankest 1176:   <li>
1.85 avankest 1177:    <p>If the state is <span title="UNSENT state">UNSENT</span>,
1.134 avankest 1178:    <span title="OPENED state">OPENED</span> and the <span><code>send()</code>
1.87 avankest 1179:    flag</span> is "false", or <span title="DONE state">DONE</span> go to
 1180:    the next step.</p>
1.85 avankest 1181: 
1.135 avankest 1182:    <p>Otherwise, switch the state to
 1183:    <span title="DONE state">DONE</span>, set the
 1184:    <span><code>send()</code> flag</span> to "false" and synchronously
 1185:    dispatch a <code>readystatechange</code> event on the object.</p>
1.83 avankest 1186:   </li>
1.67 avankest 1187: 
1.60 avankest 1188:   <li>
1.97 avankest 1189:    <p>Switch the state to <span title="UNSENT state">UNSENT</span>. (Do not
1.71 avankest 1190:    dispatch the <code>readystatechange</code> event.)</p>
1.60 avankest 1191: 
1.82 avankest 1192:    <p class="note">It is likely that a future version of the
 1193:    <code>XMLHttpRequest</code> object will dispatch an
 1194:    <code title="">abort</code> event here as well.</p>
1.60 avankest 1195:   </li>
 1196:   </ol>
1.5 avankest 1197:  </dd>
1.12 avankest 1198: 
1.131 avankest 1199:  <dt><dfn id="getallresponseheaders"><code>getAllResponseHeaders()</code></dfn>,
1.22 avankest 1200:  method</dt>
1.12 avankest 1201: 
1.5 avankest 1202:  <dd>
1.60 avankest 1203:   <p>When invoked, the user agent <em class="ct">must</em> run
1.58 avankest 1204:   the following steps:</p>
 1205: 
 1206:   <ol>
1.124 avankest 1207:   <li><p>If the state is <span title="UNSENT state">UNSENT</span> or
1.134 avankest 1208:   <span title="OPENED state">OPENED</span> raise an
1.98 avankest 1209:   <code>INVALID_STATE_ERR</code> exception and terminate these
 1210:   steps.</p></li>
1.170 avankest 1211: 
 1212:   <li><p>If the <span>error flag</span> is "true" return the empty string
1.124 avankest 1213:   and terminate these steps.</p></li>
1.96 avankest 1214: 
1.167 avankest 1215:   <li><p>Return all the HTTP headers, as a single string, with each
 1216:   header line separated by a U+000D (CR) U+000A (LF) pair excluding the
 1217:   status line.</p></li>
1.58 avankest 1218:   </ol>
1.24 avankest 1219: 
1.5 avankest 1220:   <div class="example">
1.58 avankest 1221:   <pre><code>// The following script:
1.5 avankest 1222: var client = new XMLHttpRequest();
1.14 avankest 1223: client.open("GET", "test.txt", true);
1.5 avankest 1224: client.send();
1.12 avankest 1225: client.onreadystatechange = function() {
1.179 avankest 1226: if(this.readyState == 2) {
1.12 avankest 1227:  print(this.getAllResponseHeaders());
 1228: }
 1229: }
1.5 avankest 1230: 
 1231: // ...should output something similar to the following text:
 1232: Date: 2004年10月24日 04:58:38 GMT
 1233: Server: Apache/1.3.31 (Unix)
 1234: Keep-Alive: timeout=15, max=99
 1235: Connection: Keep-Alive
 1236: Transfer-Encoding: chunked
1.58 avankest 1237: Content-Type: text/plain; charset=utf-8</code></pre>
1.5 avankest 1238:   </div>
 1239:  </dd>
1.12 avankest 1240: 
1.131 avankest 1241:  <dt><dfn id="getresponseheader" title="getresponseheader"><code>getResponseHeader(<var>header</var>)</code></dfn>,
1.22 avankest 1242:  method</dt>
1.12 avankest 1243: 
1.1 avankest 1244:  <dd>
1.58 avankest 1245:   <p>When the method is invoked, the user agent <em class="ct">must</em> run
 1246:   the following steps:</p>
1.14 avankest 1247: 
1.58 avankest 1248:   <ol>
1.124 avankest 1249:   <li><p>If the state is <span title="UNSENT state">UNSENT</span> or
1.134 avankest 1250:   <span title="OPENED state">OPENED</span> raise an
1.98 avankest 1251:   <code>INVALID_STATE_ERR</code> exception and terminate these
 1252:   steps.</p></li>
1.58 avankest 1253: 
1.149 avankest 1254:   <li><p>If the <var>header</var> argument does not match the
1.171 avankest 1255:   <span><code>field-name</code> production</span> return
 1256:   <code>null</code> and terminate these steps.</p></li>
1.124 avankest 1257: 
1.149 avankest 1258:   <li><p>If the <span>error flag</span> is "true" return
 1259:   <code>null</code> and terminate these steps.</p></li>
1.58 avankest 1260: 
1.80 avankest 1261:   <li><p>If the <var>header</var> argument
 1262:   <span title="case-insensitive match">case-insensitively matches</span>
1.127 avankest 1263:   multiple HTTP headers for the last request sent, return the values of
1.149 avankest 1264:   these headers as a single concatenated string separated from each
1.167 avankest 1265:   other by an U+002C followed by an U+0020 character and terminate
1.149 avankest 1266:   these steps.</p></li>
1.58 avankest 1267: 
1.80 avankest 1268:   <li><p>If the <var>header</var> argument
1.149 avankest 1269:   <span title="case-insensitive match">case-insensitively matches</span>
 1270:   a single HTTP header for the last request sent return the value of
 1271:   that header and terminate these steps.</p></li>
1.58 avankest 1272: 
1.96 avankest 1273:   <li><p>Return <code>null</code>.</p></li>
1.58 avankest 1274:   </ol>
1.12 avankest 1275: 
1.1 avankest 1276:   <div class="example">
1.58 avankest 1277:   <pre><code>// The following script:
1.1 avankest 1278: var client = new XMLHttpRequest();
1.14 avankest 1279: client.open("GET", "test.txt", true);
1.5 avankest 1280: client.send();
1.12 avankest 1281: client.onreadystatechange = function() {
1.179 avankest 1282: if(this.readyState == 2) {
1.14 avankest 1283:  print(client.getResponseHeader("Content-Type"));
1.12 avankest 1284: }
 1285: }
1.1 avankest 1286: 
1.5 avankest 1287: // ...should output something similar to the following text:
1.163 avankest 1288: text/plain; charset=utf-8</code></pre>
1.1 avankest 1289:   </div>
1.5 avankest 1290:  </dd>
1.12 avankest 1291: 
1.131 avankest 1292:  <dt><dfn id="responsetext"><code>responseText</code></dfn> of type
1.22 avankest 1293:  <code>DOMString</code>, readonly</dt>
1.12 avankest 1294: 
1.5 avankest 1295:  <dd>
1.88 avankest 1296:   <p>On getting, the user agent <em class="ct">must</em> run the following
 1297:   steps:</p>
 1298: 
 1299:   <ol>
1.169 avankest 1300:   <li><p>If the state is not <span title="LOADING state">LOADING</span>
 1301:   or <span title="DONE state">DONE</span> return the empty string and
 1302:   terminate these steps.</p></li>
1.12 avankest 1303: 
1.88 avankest 1304:   <li><p>Return the <span>text response entity body</span>.</p></li>
 1305:   </ol>
1.5 avankest 1306:  </dd>
1.12 avankest 1307: 
1.131 avankest 1308:  <dt><dfn id="responsexml"><code>responseXML</code></dfn> of type
1.22 avankest 1309:  <code>Document</code>, readonly</dt>
1.12 avankest 1310: 
1.8 avankest 1311:  <dd>
1.75 avankest 1312:   <p>On getting, the user agent <em class="ct">must</em> run the following
 1313:   steps:</p>
1.79 avankest 1314: 
1.75 avankest 1315:   <ol>
1.169 avankest 1316:   <li><p>If the state is not <span title="DONE state">DONE</span> return
 1317:   <code>null</code> and terminate these steps.</p></li>
1.75 avankest 1318: 
1.88 avankest 1319:   <li><p>Return the <span>XML response entity body</span>.</p></li>
1.75 avankest 1320:   </ol>
1.5 avankest 1321:  </dd>
1.12 avankest 1322: 
1.131 avankest 1323:  <dt><dfn id="status"><code>status</code></dfn> of type <code>unsigned
1.22 avankest 1324:  short</code>, readonly</dt>
1.12 avankest 1325: 
1.5 avankest 1326:  <dd>
1.72 avankest 1327:   <p>On getting, if available, it <em class="ct">must</em> return the HTTP
 1328:   status code sent by the server (typically <code>200</code> for a
 1329:   successful request). Otherwise, if not available, the user agent
 1330:   <em class="ct">must</em> raise an <code>INVALID_STATE_ERR</code>
 1331:   exception.</p>
1.12 avankest 1332:  </dd>
 1333: 
1.131 avankest 1334:  <dt><dfn id="statustext"><code>statusText</code></dfn> of type
1.22 avankest 1335:  <code>DOMString</code>, readonly</dt>
1.12 avankest 1336: 
1.5 avankest 1337:  <dd>
1.72 avankest 1338:   <p>On getting, if available, it <em class="ct">must</em> return the HTTP
 1339:   status text sent by the server (appears after the status code). Otherwise,
1.179 avankest 1340:   if not available (request is not initiated for instance), the user
 1341:   agent <em class="ct">must</em> raise an <code>INVALID_STATE_ERR</code>
 1342:   exception.</p>
1.1 avankest 1343:  </dd>
 1344:  </dl>
1.12 avankest 1345: 
1.121 avankest 1346: 
1.31 avankest 1347:  <h3 id="events">Events for the <code title="">XMLHttpRequest</code>
 1348:  Object</h3>
1.12 avankest 1349: 
1.134 avankest 1350:  <p>This section describes the various events that can be dispatched on
 1351:  objects implementing the <code>XMLHttpRequest</code> interface. For this
1.22 avankest 1352:  version of the specification only one event is defined.</p>
1.12 avankest 1353: 
1.1 avankest 1354:  <dl>
1.131 avankest 1355:  <dt><dfn id="readystatechange"><code>readystatechange</code></dfn></dt>
1.24 avankest 1356: 
1.72 avankest 1357:  <dd>When the user agent dispatches a <code title="">readystatechange</code>
 1358:  event (as indicated above) it <em class="ct">must not</em> bubble,
 1359:  <em class="ct">must not</em> be cancelable and <em class="ct">must</em>
 1360:  implement the <code>Event</code> interface. Its <code>namespaceURI</code>
 1361:  attribute <em class="ct">must</em> be <code>null</code>.
1.155 avankest 1362:  [<cite><span>DOM2Events</span></cite>]</dd>
1.1 avankest 1363:  </dl>
1.32 avankest 1364: 
1.31 avankest 1365:  <h3 id="exceptions">Exceptions for the <code title="">XMLHttpRequest</code>
 1366:  Object</h3>
1.138 avankest 1367:  
 1368:  <p>Several algorithms in this specification may result in an exception
 1369:  being thrown. These exceptions are all part of the group
1.185 avankest 1370:  <code>ExceptionCode</code> and use the <code>DOMException</code> object,
1.138 avankest 1371:  which is defined in DOM Level 3 Core. In addition this specification
 1372:  extends the <code>ExceptionCode</code> group with several new
 1373:  constants as indicated below. [<cite><span>DOM3Core</span></cite>]</p>
1.31 avankest 1374: 
1.138 avankest 1375:  <pre class="idl">const unsigned short <span>SECURITY_ERR</span> = 18;
1.121 avankest 1376: const unsigned short <span>NETWORK_ERR</span> = 101;
 1377: const unsigned short <span>ABORT_ERR</span> = 102;</pre>
1.31 avankest 1378: 
1.138 avankest 1379:  <p>The <dfn id="security-err"><code>SECURITY_ERR</code></dfn> exception is
 1380:  raised if an attempt is made to perform an operation or access some data
 1381:  in a way that would be a security risk or a violation of the user agent's
 1382:  security policy.</p>
 1383: 
 1384:  <!-- http://lists.w3.org/Archives/Public/public-webapi/2006May/0027.html -->
 1385: 
 1386:  <p class="note">The <code title="">SECURITY_ERR</code> exception is
 1387:  expected to be eventually folded into an update of the the DOM Level 3
 1388:  Core specification with an equivalent definition and identical constant
 1389:  value. Until that happens it is defined here to guide implementors. (This
1.153 avankest 1390:  is also the reason the constant value is not in line with the other
1.138 avankest 1391:  exceptions.)</p>
 1392: 
1.33 avankest 1393:  <p>The <dfn id="network-err"><code>NETWORK_ERR</code></dfn> exception is
1.138 avankest 1394:  raised when a network error occurs in synchronous requests.</p>
1.121 avankest 1395: 
1.138 avankest 1396:  <p>The <dfn id="abort-err"><code>ABORT_ERR</code></dfn> exception is
 1397:  raised when the user aborts a request in synchronous requests.</p>
1.121 avankest 1398: 
 1399: 
1.29 avankest 1400:  <h2 class="no-num" id="notcovered">Not in this Specification</h2>
 1401: 
1.143 avankest 1402:  <p><em>This section is non-normative.</em></p>
1.29 avankest 1403: 
1.72 avankest 1404:  <p>This specification does not include the following features which are
 1405:  being considered for a future version of this specification:</p>
1.29 avankest 1406: 
 1407:  <ul>
 1408:  <li><code>load</code> event and <code>onload</code> attribute;</li>
 1409:  <li><code>error</code> event and <code>onerror</code> attribute;</li>
 1410:  <li><code>progress</code> event and <code>onprogress</code> attribute;</li>
 1411:  <li><code title="">abort</code> event and <code>onabort</code> attribute;</li>
 1412:  <li>Timers have been suggested, perhaps an <code>ontimeout</code>
 1413:   attribute;</li>
 1414:  <li>Property to disable following redirects;</li>
1.30 avankest 1415:  <li><code title="">responseXML</code> for <code>text/html</code>
 1416:  documents;</li>
1.40 avankest 1417:  <li>Cross-site <code title="">XMLHttpRequest</code>;</li>
1.87 avankest 1418:  <li><code>responseBody</code> to deal with byte streams;</li>
1.114 avankest 1419:  <li><code>overrideMimeType</code> to fix up MIME types;</li>
1.87 avankest 1420:  <li><code>getRequestHeader()</code> and
 1421:  <code>removeRequestHeader()</code>.</li>
1.29 avankest 1422:  </ul>
 1423: 
1.1 avankest 1424:  <h2 class="no-num" id="bibref">References</h2>
1.177 avankest 1425:  
 1426:  <p>Unless marked "Non-normative" these references are normative.</p>
1.11 avankest 1427: 
1.6 avankest 1428:  <dl>
1.155 avankest 1429:  <dt>[<dfn id="ref-dom2events">DOM2Events</dfn>]</dt>
 1430:  <dd><cite><a href="http://www.w3.org/TR/DOM-Level-2-Events/">Document
1.160 avankest 1431:  Object Model (DOM) Level 2 Events Specification</a></cite>, T. Pixley,
 1432:  editor. W3C, November 2000.</dd>
1.155 avankest 1433: 
1.145 avankest 1434:  <dt>[<dfn id="ref-dom3core">DOM3Core</dfn>]</dt>
1.11 avankest 1435:  <dd><cite><a href="http://www.w3.org/TR/DOM-Level-3-Core">Document Object
1.92 avankest 1436:  Model (DOM) Level 3 Core Specification</a></cite>, A. Le Hors, P. Le
1.139 avankest 1437:  Hégaret, L. Wood, G. Nicol, J. Robie, M. Champion, S. Byrne,
 1438:  editors. W3C, April 2004.</dd>
1.11 avankest 1439: 
1.117 avankest 1440:  <dt>[<dfn id="ref-ecmascript">ECMAScript</dfn>]</dt>
1.92 avankest 1441:  <dd><cite><a href="http://www.ecma-international.org/publications/standards/Ecma-262.htm">ECMAScript
 1442:  Language Specification</a></cite>, Third Edition. ECMA, December 1999.</dd>
1.171 avankest 1443: 
1.145 avankest 1444:  <dt>[<dfn id="ref-html5">HTML5</dfn>]</dt>
1.171 avankest 1445:  <dd><cite><a href="http://www.w3.org/html/wg/html5/">HTML 5</a></cite>
 1446:  (work in progress), I. Hickson, D. Hyatt, editors. W3C, 2008.</dd>
1.142 avankest 1447:  <dd><cite><a href="http://www.whatwg.org/specs/web-apps/current-work/">HTML 5</a></cite>
1.171 avankest 1448:  (work in progress), I. Hickson, editor. WHATWG, 2008.</dd>
1.177 avankest 1449:  
 1450:  <dt>[<dfn id="ref-httponly">HTTPONLY</dfn>]</dt>
 1451:  <dd>(Non-normative) <cite><a href="http://msdn.microsoft.com/en-us/library/ms533046.aspx">Mitigating
 1452:  Cross-site Scripting With HTTP-only Cookies</a></cite>, MSDN.</dd>
1.14 avankest 1453: 
1.178 avankest 1454:  <dt>[<dfn id="ref-rfc2109">RFC2109</dfn>]</dt>
 1455: 
 1456:  <dd><cite><a href="http://ietf.org/rfc/rfc2109">HTTP State Management
 1457:  Mechanism</a></cite>, D. Kristol, L. Montulli, editors. IETF, February
 1458:  1997.</dd>
 1459: 
1.145 avankest 1460:  <dt>[<dfn id="ref-rfc2119">RFC2119</dfn>]</dt>
1.117 avankest 1461:  <dd><cite><a href="http://ietf.org/rfc/rfc2119">Key words for use in RFCs to
 1462:  Indicate Requirement Levels</a></cite>, S. Bradner. IETF, March 1997.</dd>
1.12 avankest 1463: 
1.145 avankest 1464:  <dt>[<dfn id="ref-rfc2616">RFC2616</dfn>]</dt>
1.11 avankest 1465:  <dd><cite><a href="http://ietf.org/rfc/rfc2616">Hypertext Transfer Protocol
1.92 avankest 1466:  -- HTTP/1.1</a></cite>, R. Fielding, J. Gettys, J. Mogul, H. Frystyk, L.
 1467:  Masinter, P. Leach, T. Berners-Lee, editors. IETF, June 1999.</dd>
1.12 avankest 1468: 
1.37 avankest 1469:  <dt>[<dfn id="ref-rfc2617">RFC2617</dfn>]</dt>
1.11 avankest 1470:  <dd><cite><a href="http://ietf.org/rfc/rfc2617">HTTP Authentication: Basic
1.92 avankest 1471:  and Digest Access Authentication</a></cite>, P. Hallam-Baker, J.
 1472:  Hostetler, S. Lawrence, P. Leach, A. Luotonen, L. Stewart, editors. IETF,
 1473:  June 1999.</dd>
1.12 avankest 1474: 
1.37 avankest 1475:  <dt>[<dfn id="ref-rfc2965">RFC2965</dfn>]</dt>
1.19 avankest 1476:  <dd><cite><a href="http://ietf.org/rfc/rfc2965">HTTP State Management
1.92 avankest 1477:  Mechanism</a></cite>, D. Kristol, L. Montulli, editors. IETF, October
 1478:  2000.</dd>
1.19 avankest 1479: 
1.37 avankest 1480:  <dt>[<dfn id="ref-rfc3986">RFC3986</dfn>]</dt>
1.11 avankest 1481:  <dd><cite><a href="http://ietf.org/rfc/rfc3986">Uniform Resource Identifier
1.92 avankest 1482:  (URI): Generic Syntax</a></cite>, T. Berners-Lee, R. Fielding, L. Masinter,
 1483:  editors. IETF, January 2005.</dd>
1.184 avankest 1484: 
 1485:  <dt>[<dfn id=ref-rfc3987>RFC3987</dfn>]</dt>
 1486: 
1.141 avankest 1487:  <dd><cite><a href="http://ietf.org/rfc/rfc3987">Internationalized
 1488:  Resource Identifiers (IRIs)</a></cite>, M. Duerst, M. Suignard, editors.
1.184 avankest 1489:  IETF, January 2005. L. Masinter, editors. IETF, January 2005.</dd>
1.28 avankest 1490: 
1.181 avankest 1491:  <dt>[<dfn id="ref-webidl">Web IDL</dfn>]</dt>
 1492:  <dd><cite><a href="http://dev.w3.org/2006/webapi/Binding4DOM/">Web
 1493:  IDL</a></cite> (editor's draft), C. McCormack, editor. W3C, 2008.</dd>
 1494: 
1.92 avankest 1495:  <dt>[<dfn id="ref-xml">XML</dfn>]</dt>
 1496:  <dd><cite><a href="http://www.w3.org/TR/xml/">Extensible Markup Language
 1497:  (XML) 1.0 (Fourth Edition)</a></cite>, T. Bray, J. Paoli, C.
1.117 avankest 1498:  Sperberg-McQueen, E. Maler, F. Yergeau, editors. W3C, September 2006.</dd>
1.41 avankest 1499: 
1.92 avankest 1500:  <dt>[<dfn id="ref-xmlns">XMLNS</dfn>]</dt>
 1501:  <dd><cite><a href="http://www.w3.org/TR/xml-names/">Namespaces in XML
1.117 avankest 1502:  (Second Edition)</a></cite>, T. Bray, D. Hollander, A. Layman, R. Tobin,
 1503:  editors. W3C, August 2006.</dd>
1.1 avankest 1504:  </dl>
1.11 avankest 1505: 
1.130 avankest 1506:  <h2 class="no-num" id="acknowledgments">Acknowledgments</h2>
1.11 avankest 1507: 
1.159 avankest 1508:  <p>The editor would like to thank
1.12 avankest 1509: 
1.163 avankest 1510:  Addison Phillips,
1.159 avankest 1511:  Ahmed Kamel,
 1512:  Alex Hopmann,
 1513:  Alex Vincent,
 1514:  Alexey Proskuryakov,
 1515:  Asbjørn Ulsberg,
 1516:  Boris Zbarsky,
 1517:  Björn Höhrmann,
 1518:  Cameron McCormack,
 1519:  Christophe Jolif,
 1520:  Charles McCathieNevile,
 1521:  Dan Winship,
 1522:  David Håsäther,
 1523:  Dean Jackson,
 1524:  Denis Sureau,
 1525:  Doug Schepers,
 1526:  Douglas Livingstone,
 1527:  Elliotte Harold,
 1528:  Eric Lawrence,
1.163 avankest 1529:  Geoffrey Sneddon,
1.159 avankest 1530:  Gideon Cohn,
 1531:  Gorm Haug Eriksen,
 1532:  Hallvord R. M. Steen,
 1533:  Håkon Wium Lie,
 1534:  Ian Davis,
 1535:  Ian Hickson,
 1536:  Ivan Herman,
 1537:  Jeff Walden,
 1538:  Jens Lindström,
 1539:  Jim Deegan,
 1540:  Jim Ley,
 1541:  Joe Farro,
 1542:  Jonas Sicking,
 1543:  Julian Reschke,
 1544:  Karl Dubost,
1.175 avankest 1545:  Lachlan Hunt,
1.159 avankest 1546:  Maciej Stachowiak,
 1547:  Magnus Kristiansen,
 1548:  Marc Hadley,
 1549:  Marcos Caceres,
 1550:  Mark Baker,
 1551:  Mark Nottingham,
 1552:  Mohamed Zergaoui,
 1553:  Pawel Glowacki,
 1554:  Robin Berjon,
 1555:  Ruud Steltenpool,
 1556:  Simon Pieters,
 1557:  Stewart Brodie,
 1558:  Sunava Dutta,
1.186 avankest 1559:  Thomas Roessler,
1.159 avankest 1560:  Tom Magliery and
 1561:  Zhenbin Xu
1.181 avankest 1562: 
1.159 avankest 1563:  for their contributions to this specification.</p>
1.12 avankest 1564: 
1.1 avankest 1565:  <p>Special thanks to the Microsoft employees who first implemented the
1.143 avankest 1566:  <code title="">XMLHttpRequest</code> interface, which was first widely
 1567:  deployed by the Windows Internet Explorer browser.</p>
1.12 avankest 1568: 
1.143 avankest 1569:  <p>Special thanks also to the WHATWG for drafting an initial version of
 1570:  this specification in their Web Applications 1.0 document (now renamed to
 1571:  HTML 5). [<cite><span>HTML5</span></cite>]</p>
1.12 avankest 1572: 
1.143 avankest 1573:  <p>Thanks also to all those who have helped to improve this specification
 1574:  by sending suggestions and corrections. (Please, keep bugging us with your
1.22 avankest 1575:  issues!)</p>
1.1 avankest 1576: </body>
 1577: </html>
Webmaster