mozilla

Revision 14339 of XMLHttpRequest

  • Revision slug: DOM/XMLHttpRequest
  • Revision title: XMLHttpRequest
  • Revision id: 14339
  • Created:
  • Creator: Konno.Software
  • Is current revision? No
  • Comment no wording changes

Revision Content

XMLHttpRequest is a JavaScript object that was designed by Microsoft, adopted by Mozilla, and is now being standardized in the W3C.  It provides an easy way to retrieve data at a URL.  Despite its name, XMLHttpRequest can be used to retrieve any type of data, not just XML, and it supports connections other than HTTP (including file and ftp).

To create an instance of XMLHttpRequest, simply do this:

var req = new XMLHttpRequest();

For details about how to use XMLHttpRequest, see Using XMLHttpRequest.

Note: By default, Firefox 3 limits the number of XMLHttpRequest connections per server to 6 (previous versions limit this to 2 per server).  Some interactive web sites may keep an XMLHttpRequest connection open, so opening multiple sessions to such sites may result in the browser hanging in such a way that the window no longer repaints and controls don't respond.  This value can be changed by editing the network.http.max-persistent-connections-per-server preference in about:config.

Method overview

void abort();
string getAllResponseHeaders();
ACString getResponseHeader(in AUTF8String header);
[noscript] void init(in nsIPrincipal principal, in nsIScriptContext scriptContext, in nsPIDOMWindow ownerWindow);
void open(in AUTF8String method, in AUTF8String url);
[noscript] void openRequest(in AUTF8String method, in AUTF8String url, in boolean async, in AString user, in AString password);
void overrideMimeType(in AUTF8String mimetype);
void send([optional] in nsIVariant body);
void sendAsBinary(in DOMString body); {{ fx_minversion_inline("3") }}
void setRequestHeader(in AUTF8String header, in AUTF8String value);

Attributes

Attribute Type Description
channel nsIChannel  The channel used by the object when performing the request.  This is null if the channel hasn't been created yet.  In the case of a multi-part request, this is the initial channel, not the different parts in the multi-part request.  Requires elevated privileges to access; read-only. {{ Non-standard_inline() }}
mozBackgroundRequest boolean

Indicates whether or not the object represents a background service request.  If true, no load group is associated with the request, and security dialogs are prevented from being shown to the user.  {{ Non-standard_inline() }}

In cases in which a security dialog (such as authentication or a bad certificate notification) would normally be shown, the request simply fails instead.

multipart boolean

Indicates whether or not the response is expected to be a stream of possibly multiple XML documents.  If set to true, the content type of the initial response must be multipart/x-mixed-replace or an error will occur.  All requests must be asynchronous.

This enables support for server push; for each XML document that's written to this request, a new XML DOM document is created and the onload handler is called between documents.

Note: When this is set, the onload handler and other event handlers are not reset after the first XML document is loaded, and the onload handler is called after each part of the response is received.

onreadystatechange

nsIDOMEventListener

A JavaScript function object that is called whenever the readyState attribute changes.  The callback is called from the user interface thread.

You need to call open() before setting this attribute.

Warning: This must not be used from native code.  You should also not use this with synchronous requests.
readyState long

The state of the request:

Value State Description
0 UNINITIALIZED open() has not been called yet.
1 LOADING send() has not been called yet.
2 LOADED send() has been called, and headers and status are available.
3 INTERACTIVE Downloading; responseText holds partial data.
4 COMPLETED The operation is complete.
responseText AString The response to the request as text, or null if the request was unsucessful or has not yet been sent.  Read-only.
responseXML nsIDOMDocument

The response to the request as a DOM Document object, or null if the request was unsuccessful, has not yet been sent, or cannot be parsed as XML.  The response is parsed as if it were a text/xml stream.  Read-only.

Note: If the server doesn't apply the text/xml Content-Type header, you can use overrideMimeType() to force XMLHttpRequest to parse it as XML anyway.
status unsigned long The status of the response to the request.  This is the HTTP result code (for example, status is 200 for a successful request).  Read-only.
statusText AUTF8String The response string returned by the HTTP server.  Unlike status, this includes the entire text of the response message ("200 OK", for example).  Read-only.
upload nsIXMLHttpRequestUpload The upload process can be tracked by adding an event listener to upload.  {{ fx_minversion_inline("3") }}
withCredentials boolean

Indicates whether or not cross-site Access-Control requests should be made using credentials such as cookies or authorization headers.  {{ fx_minversion_inline("3") }}

Note: This never affects same-site requests.

The default is false.

 

Methods

abort()

Aborts the request if it has already been sent.

void abort();
Parameters

None.

getAllResponseHeaders()

Returns all the response headers as a string.

Note: For multipart requests, this returns the headers from the current part of the request, not from the original channel.
string getAllResponseHeaders();
Parameters

None.

Return value

The text of all response headers, or null if no response has been received.

getResponseHeader()

Returns the text of a specified header.

ACString getResponseHeader(
  in AUTF8String header
);
Parameters
header
The name of the header to retrieve.
Return value

A string containing the text of the specified header, or null if either the response has not yet been received or the header doesn't exist in the response.

init()

Initializes the object for use from C++ code.

Warning: This method must not be called from JavaScript.
[noscript] void init(
  in nsIPrincipal principal,
  in nsIScriptContext scriptContext,
  in nsPIDOMWindow ownerWindow
);
Parameters
principal
The principal to use for the request; must not be null.
scriptContext
The script context to use for the request; must not be null.
ownerWindow
The window associated with the request; may be null.

open()

Initializes a request.  This method is to be used from JavaScript code; to initialize a request from native code, use openRequest() instead.

Note: Calling this method an already active request (one for which open() or openRequest() has already been called) is the equivalent of calling abort().
void open(
  in AUTF8String method,
  in AUTF8String url,
  [optional] in boolean async,
  [optional] in AString user,
  [optional] in AString password
);
Parameters
method
The HTTP method to use; either "POST" or "GET".  Ignored for non-HTTP URLs.
url
The URL to which to send the request.
async
An optional boolean parameter, defaulting to true, indicating whether or not to perform the operation asynchronously.  If this value is false, the send() method does not return until the response is received.  If true, notification of a completed transaction is provided using event listeners.  This must be true if the multipart attribute is true, or an exception will be thrown.
user
The optional user name to use for authentication purposes; by default, this is an empty string.
password
The optional password to use for authentication purposes; by default, this is an empty string.

openRequest()

Initializes a request.  This method is to be used from native code; to initialize a request from JavaScript code, use open() instead.

Note: Calling this method an already active request (one for which open() or openRequest() has already been called) is the equivalent of calling abort().
void open(
  in AUTF8String method,
  in AUTF8String url,
  in boolean async,
  in AString user,
  in AString password
);
Parameters
method
The HTTP method to use; either "POST" or "GET".  Ignored for non-HTTP URLs.
url
The URL to which to send the request.
async
An optional boolean parameter, defaulting to true, indicating whether or not to perform the operation asynchronously.  If this value is false, the send() method does not return until the response is received.  If true, notification of a completed transaction is provided using event listeners.  This must be true if the multipart attribute is true, or an exception will be thrown.
user
The optional user name to use for authentication purposes; by default, this is an empty string.
password
The optional password to use for authentication purposes; by default, this is an empty string.

overrideMimeType()

Overrides the MIME type returned by the server.

Note: This method must be called before send().
void overrideMimeType(
  in AUTF8String mimetype
);
Parameters
mimetype
The type that should be used instead of the one returned by the server, if any.

send()

Sends the request.  If the request is asynchronous (which is the default), this method returns as soon as the request is sent.  If the request is synchronous, this method doesn't return until the response has arrived.

Note: Any event listeners you wish to set must be set before calling send().
void send(
  [optional] in nsIVariant body
);
Parameters
body
This may be an nsIDocument, nsIInputStream, or a string (an nsISupportsString if called from native code) that is used to populate the body of a POST request.
Notes

If the body is an nsIDOMDocument, it is serialized before being sent.

If it's an nsIInputStream, it must be compatible with nsIUploadChannel's setUploadStream() method.  In that case, a Content-Length header is added to the request, with its value obtained using nsIInputStream's available() method.  Any headers included at the top of the stream are treated as part of the message body.  The stream's MIME type should be specified by setting the Content-Type header using the setRequestHeader() method prior to calling send().

sendAsBinary()

{{ fx_minversion_note("3") }}

A variant of the send() method that sends binary data.

void sendAsBinary(
  in DOMString body
);
Parameters
body
The request body as a DOM string.  This data is converted to a string of single-byte characters by truncation (removing the high-order byte of each character).

setRequestHeader()

Sets the value of an HTTP request header.

Note: You must call open() before using this method.
void setRequestHeader(
  in AUTF8String header,
  in AUTF8String value
);
Parameters
header
The name of the header whose value is to be set.
value
The value to set as the body of the header.

Implementation notes

XMLHttpRequest is implemented in Gecko using the {{ interface("nsIJSXMLHttpRequest") }} and {{ interface("nsIXMLHttpRequest") }} interfaces.

See also

{{ languages( { "es": "es/XMLHttpRequest", "fr": "fr/XMLHttpRequest", "it": "it/XMLHttpRequest", "ja": "ja/XMLHttpRequest", "ko": "ko/XMLHttpRequest", "pl": "pl/XMLHttpRequest", "zh-cn": "cn/XMLHttpRequest" } ) }}

Revision Source

<p><code>XMLHttpRequest</code> is a <a class="internal" href="/en/JavaScript" title="En/JavaScript">JavaScript</a> object that was designed by Microsoft, adopted by Mozilla, and is now being <a class="external" href="http://www.w3.org/TR/XMLHttpRequest/" title="http://www.w3.org/TR/XMLHttpRequest/">standardized in the W3C</a>.  It provides an easy way to retrieve data at a URL.  Despite its name, <code>XMLHttpRequest</code> can be used to retrieve any type of data, not just XML, and it supports connections other than HTTP (including <code>file</code> and <code>ftp</code>).</p>
<p>To create an instance of <code>XMLHttpRequest</code>, simply do this:</p>
<pre>var req = new XMLHttpRequest();
</pre>
<p>For details about how to use <code>XMLHttpRequest</code>, see <a class="internal" href="/En/Using_XMLHttpRequest" title="En/Using XMLHttpRequest">Using XMLHttpRequest</a>.</p>
<div class="note"><strong>Note:</strong> By default, Firefox 3 limits the number of <code>XMLHttpRequest</code> connections per server to 6 (previous versions limit this to 2 per server).  Some interactive web sites may keep an <code>XMLHttpRequest</code> connection open, so opening multiple sessions to such sites may result in the browser hanging in such a way that the window no longer repaints and controls don't respond.  This value can be changed by editing the <code>network.http.max-persistent-connections-per-server</code> preference in <code>about:config</code>.</div>
<h2>Method overview</h2>
<table class="standard-table"> <tbody> <tr> <td><code>void <a class="internal" href="/en/XMLHttpRequest#abort()" title="/En/XMLHttpRequest#abort()">abort</a>();</code></td> </tr> <tr> <td><code>string <a class="internal" href="/en/XMLHttpRequest#getAllResponseHeaders()" title="/en/XMLHttpRequest#getAllResponseHeaders()">getAllResponseHeaders</a>();</code></td> </tr> <tr> <td><code>ACString <a class="internal" href="/en/XMLHttpRequest#getResponseHeader()" title="/en/XMLHttpRequest#getResponseHeader()">getResponseHeader</a>(in AUTF8String header);</code></td> </tr> <tr> <td><code>[noscript] void <a class="internal" href="/en/XMLHttpRequest#init()" title="/en/XMLHttpRequest#init()">init</a>(in nsIPrincipal principal, in nsIScriptContext scriptContext, in nsPIDOMWindow ownerWindow);</code></td> </tr> <tr> <td><code>void <a class="internal" href="/en/XMLHttpRequest#open()" title="/en/XMLHttpRequest#open()">open</a>(in AUTF8String method, in AUTF8String url);</code></td> </tr> <tr> <td><code>[noscript] void <a class="internal" href="/en/XMLHttpRequest#openRequest()" title="/en/XMLHttpRequest#openRequest()">openRequest</a>(in AUTF8String method, in AUTF8String url, in boolean async, in AString user, in AString password);</code></td> </tr> <tr> <td><code>void <a class="internal" href="/en/XMLHttpRequest#overrideMimeType()" title="/en/XMLHttpRequest#overrideMimeType()">overrideMimeType</a>(in AUTF8String mimetype);</code></td> </tr> <tr> <td><code>void <a class="internal" href="/en/XMLHttpRequest#send()" title="/en/XMLHttpRequest#send()">send</a>([optional] in nsIVariant body);</code></td> </tr> <tr> <td><code>void <a class="internal" href="/en/XMLHttpRequest#sendAsBinary()" title="/en/XMLHttpRequest#sendAsBinary()">sendAsBinary</a>(in DOMString body); {{ fx_minversion_inline("3") }}<br> </code></td> </tr> <tr> <td><code>void <a class="internal" href="/en/XMLHttpRequest#setRequestHeader()" title="/en/XMLHttpRequest#setRequestHeader()">setRequestHeader</a>(in AUTF8String header, in AUTF8String value);</code></td> </tr> </tbody>
</table>
<h2>Attributes</h2>
<table class="standard-table"> <tbody> <tr> <td class="header">Attribute</td> <td class="header">Type</td> <td class="header">Description</td> </tr> <tr> <td><code>channel</code></td> <td><a class="internal" href="/en/nsIChannel" title="En/NsIChannel"><code>nsIChannel</code></a> </td> <td>The channel used by the object when performing the request.  This is <code>null</code> if the channel hasn't been created yet.  In the case of a multi-part request, this is the initial channel, not the different parts in the multi-part request.  <strong>Requires elevated privileges to access; read-only.</strong> {{ Non-standard_inline() }}</td> </tr> <tr> <td><code>mozBackgroundRequest</code></td> <td><code>boolean</code></td> <td> <p>Indicates whether or not the object represents a background service request.  If <code>true</code>, no load group is associated with the request, and security dialogs are prevented from being shown to the user.  {{ Non-standard_inline() }}</p> <p>In cases in which a security dialog (such as authentication or a bad certificate notification) would normally be shown, the request simply fails instead.</p> </td> </tr> <tr> <td><code>multipart</code></td> <td><code>boolean</code></td> <td> <p>Indicates whether or not the response is expected to be a stream of possibly multiple XML documents.  If set to <code>true</code>, the content type of the initial response must be <code>multipart/x-mixed-replace</code> or an error will occur.  All requests must be asynchronous.</p> <p>This enables support for server push; for each XML document that's written to this request, a new XML DOM document is created and the <code>onload</code> handler is called between documents.</p> <div class="note"><strong>Note:</strong> When this is set, the <code>onload</code> handler and other event handlers are not reset after the first XML document is loaded, and the <code>onload</code> handler is called after each part of the response is received.</div> </td> </tr> <tr> <td> <p><code>onreadystatechange</code></p> </td> <td><a class="internal" href="/en/nsIDOMEventListener" title="En/nsIDOMEventListener"><code>nsIDOMEventListener</code></a></td> <td> <p>A JavaScript function object that is called whenever the <code>readyState</code> attribute changes.  The callback is called from the user interface thread.</p> <p>You need to call <code>open()</code> before setting this attribute.</p> <div class="warning"><strong>Warning: </strong>This must not be used from native code.  You should also not use this with synchronous requests.</div> </td> </tr> <tr id="readyState"> <td><code>readyState</code></td> <td><code>long</code></td> <td> <p>The state of the request:</p> <table class="standard-table"> <tbody> <tr> <td class="header">Value</td> <td class="header">State</td> <td class="header">Description</td> </tr> <tr> <td><code>0</code></td> <td><code>UNINITIALIZED</code></td> <td><code>open()</code> has not been called yet.</td> </tr> <tr> <td><code>1</code></td> <td><code>LOADING</code></td> <td><code>send()</code> has not been called yet.</td> </tr> <tr> <td><code>2</code></td> <td><code>LOADED</code></td> <td><code>send()</code> has been called, and headers and status are available.</td> </tr> <tr> <td><code>3</code></td> <td><code>INTERACTIVE</code></td> <td>Downloading; <code>responseText</code> holds partial data.</td> </tr> <tr> <td><code>4</code></td> <td><code>COMPLETED</code></td> <td>The operation is complete.</td> </tr> </tbody> </table> </td> </tr> <tr> <td><code>responseText</code></td> <td><code>AString</code></td> <td>The response to the request as text, or <code>null</code> if the request was unsucessful or has not yet been sent.  <strong>Read-only.</strong></td> </tr> <tr> <td><code>responseXML</code></td> <td><code>nsIDOMDocument</code></td> <td> <p>The response to the request as a DOM <code><a class="internal" href="/en/DOM/document" title="En/DOM/Document">Document</a></code> object, or <code>null</code> if the request was unsuccessful, has not yet been sent, or cannot be parsed as XML.  The response is parsed as if it were a <code>text/xml</code> stream.  <strong>Read-only.</strong></p> <div class="note"><strong>Note:</strong> If the server doesn't apply the <code>text/xml</code> Content-Type header, you can use <code>overrideMimeType()</code> to force <code>XMLHttpRequest</code> to parse it as XML anyway.</div> </td> </tr> <tr id="status"> <td><code>status</code></td> <td><code>unsigned long</code></td> <td>The status of the response to the request.  This is the HTTP result code (for example, <code>status</code> is 200 for a successful request).  <strong>Read-only.</strong></td> </tr> <tr> <td><code>statusText</code></td> <td><code>AUTF8String</code></td> <td>The response string returned by the HTTP server.  Unlike <code>status</code>, this includes the entire text of the response message ("<code>200 OK</code>", for example).  <strong>Read-only.</strong></td> </tr> <tr> <td><code>upload</code></td> <td><code>nsIXMLHttpRequestUpload</code></td> <td>The upload process can be tracked by adding an event listener to <code>upload</code>.  {{ fx_minversion_inline("3") }}</td> </tr> <tr> <td><code>withCredentials</code></td> <td><code>boolean</code></td> <td> <p>Indicates whether or not cross-site Access-Control requests should be made using credentials such as cookies or authorization headers.  {{ fx_minversion_inline("3") }}</p> <div class="note"><strong>Note:</strong> This never affects same-site requests.</div> <p>The default is <code>false</code>.</p> </td> </tr> </tbody>
</table>
<p> </p>
<h2>Methods</h2>
<h3>abort()</h3>
<p>Aborts the request if it has already been sent.</p>
<pre>void abort();
</pre>
<h6>Parameters</h6>
<p>None.</p>
<h3>getAllResponseHeaders()</h3>
<p>Returns all the response headers as a string.</p>
<div class="note"><strong>Note:</strong> For multipart requests, this returns the headers from the <em>current</em> part of the request, not from the original channel.</div>
<pre>string getAllResponseHeaders();
</pre>
<h6>Parameters</h6>
<p>None.</p>
<h6>Return value</h6>
<p>The text of all response headers, or <code>null</code> if no response has been received.</p>
<h3>getResponseHeader()</h3>
<p>Returns the text of a specified header.</p>
<pre>ACString getResponseHeader(
  in AUTF8String header
);
</pre>
<h6>Parameters</h6>
<dl> <dt><code>header</code></dt> <dd>The name of the header to retrieve.</dd>
</dl>
<h6>Return value</h6>
<p>A string containing the text of the specified header, or <code>null</code> if either the response has not yet been received or the header doesn't exist in the response.</p>
<h3>init()</h3>
<p>Initializes the object for use from C++ code.</p>
<div class="warning"><strong>Warning:</strong> This method must <em>not</em> be called from JavaScript.</div>
<pre>[noscript] void init(
  in nsIPrincipal principal,
  in nsIScriptContext scriptContext,
  in nsPIDOMWindow ownerWindow
);
</pre>
<h6>Parameters</h6>
<dl> <dt><code>principal</code></dt> <dd>The principal to use for the request; must not be <code>null</code>.</dd> <dt><code>scriptContext</code></dt> <dd>The script context to use for the request; must not be <code>null</code>.</dd> <dt><code>ownerWindow</code></dt> <dd>The window associated with the request; may be <code>null</code>.</dd>
</dl>
<h3>open()</h3>
<p>Initializes a request.  This method is to be used from JavaScript code; to initialize a request from native code, use <a class="internal" href="/en/XMLHttpRequest#openRequest()" title="/en/XMLHttpRequest#openRequest()"><code>openRequest()</code></a> instead.</p>
<div class="note"><strong>Note:</strong> Calling this method an already active request (one for which <code>open()</code> or <code>openRequest()</code> has already been called) is the equivalent of calling <code>abort()</code>.</div>
<pre>void open(
  in AUTF8String method,
  in AUTF8String url,
  [optional] in boolean async,
  [optional] in AString user,
  [optional] in AString password
);
</pre>
<h6>Parameters</h6>
<dl> <dt><code>method</code></dt> <dd>The HTTP method to use; either "POST" or "GET".  Ignored for non-HTTP URLs.</dd> <dt><code>url</code></dt> <dd>The URL to which to send the request.</dd> <dt><code>async</code></dt> <dd>An optional boolean parameter, defaulting to <code>true</code>, indicating whether or not to perform the operation asynchronously.  If this value is <code>false</code>, the <code>send()</code> method does not return until the response is received.  If <code>true</code>, notification of a completed transaction is provided using event listeners.  This <em>must</em> be true if the <code>multipart</code> attribute is <code>true</code>, or an exception will be thrown.</dd> <dt><code>user</code></dt> <dd>The optional user name to use for authentication purposes; by default, this is an empty string.</dd> <dt><code>password</code></dt> <dd>The optional password to use for authentication purposes; by default, this is an empty string.</dd>
</dl>
<h3>openRequest()</h3>
<p>Initializes a request.  This method is to be used from native code; to initialize a request from JavaScript code, use <a class="internal" href="/en/XMLHttpRequest#open()" title="/en/XMLHttpRequest#open()"><code>open()</code></a> instead.</p>
<div class="note"><strong>Note:</strong> Calling this method an already active request (one for which <code>open()</code> or <code>openRequest()</code> has already been called) is the equivalent of calling <code>abort()</code>.</div>
<pre>void open(
  in AUTF8String method,
  in AUTF8String url,
  in boolean async,
  in AString user,
  in AString password
);
</pre>
<h6>Parameters</h6>
<dl> <dt><code>method</code></dt> <dd>The HTTP method to use; either "POST" or "GET".  Ignored for non-HTTP URLs.</dd> <dt><code>url</code></dt> <dd>The URL to which to send the request.</dd> <dt><code>async</code></dt> <dd>An optional boolean parameter, defaulting to <code>true</code>, indicating whether or not to perform the operation asynchronously.  If this value is <code>false</code>, the <code>send()</code> method does not return until the response is received.  If <code>true</code>, notification of a completed transaction is provided using event listeners.  This <em>must</em> be true if the <code>multipart</code> attribute is <code>true</code>, or an exception will be thrown.</dd> <dt><code>user</code></dt> <dd>The optional user name to use for authentication purposes; by default, this is an empty string.</dd> <dt><code>password</code></dt> <dd>The optional password to use for authentication purposes; by default, this is an empty string.</dd>
</dl>
<h3>overrideMimeType()</h3>
<p>Overrides the MIME type returned by the server.</p>
<div class="note"><strong>Note:</strong> This method must be called before <code>send()</code>.</div>
<pre>void overrideMimeType(
  in AUTF8String mimetype
);
</pre>
<h6>Parameters</h6>
<dl> <dt><code>mimetype</code></dt> <dd>The type that should be used instead of the one returned by the server, if any.</dd>
</dl>
<h3>send()</h3>
<p>Sends the request.  If the request is asynchronous (which is the default), this method returns as soon as the request is sent.  If the request is synchronous, this method doesn't return until the response has arrived.</p>
<div class="note"><strong>Note:</strong> Any event listeners you wish to set must be set before calling <code>send()</code>.</div>
<pre>void send(
  [optional] in nsIVariant body
);
</pre>
<h6>Parameters</h6>
<dl> <dt><code>body</code></dt> <dd>This may be an <code>nsIDocument</code>, <code>nsIInputStream</code>, or a string (an <code>nsISupportsString</code> if called from native code) that is used to populate the body of a POST request.</dd>
</dl>
<h6>Notes</h6>
<p>If the body is an <code>nsIDOMDocument</code>, it is serialized before being sent.</p>
<p>If it's an <code>nsIInputStream</code>, it must be compatible with <code>nsIUploadChannel</code>'s <code>setUploadStream()</code> method.  In that case, a Content-Length header is added to the request, with its value obtained using <code>nsIInputStream</code>'s <code>available()</code> method.  Any headers included at the top of the stream are treated as part of the message body.  The stream's MIME type should be specified by setting the Content-Type header using the <a class="internal" href="/en/XMLHttpRequest#setRequestHeader()" title="/en/XMLHttpRequest#setRequestHeader()"><code>setRequestHeader()</code></a> method prior to calling <code>send()</code>.</p>
<h3>sendAsBinary()</h3>
<p>{{ fx_minversion_note("3") }}</p>
<p>A variant of the <code>send()</code> method that sends binary data.</p>
<pre>void sendAsBinary(
  in DOMString body
);
</pre>
<h6>Parameters</h6>
<dl> <dt><code>body</code></dt> <dd>The request body as a DOM string.  This data is converted to a string of single-byte characters by truncation (removing the high-order byte of each character).</dd>
</dl>
<h3>setRequestHeader()</h3>
<p>Sets the value of an HTTP request header.</p>
<div class="note"><strong>Note:</strong> You must call <a class="internal" href="/en/XMLHttpRequest#open()" title="/en/XMLHttpRequest#open()"><code>open()</code></a> before using this method.</div>
<pre>void setRequestHeader(
  in AUTF8String header,
  in AUTF8String value
);
</pre>
<h6>Parameters</h6>
<dl> <dt><code>header</code></dt> <dd>The name of the header whose value is to be set.</dd> <dt><code>value</code></dt> <dd>The value to set as the body of the header.</dd>
</dl>
<h2>Implementation notes</h2>
<p><code>XMLHttpRequest</code> is implemented in Gecko using the {{ interface("nsIJSXMLHttpRequest") }} and {{ interface("nsIXMLHttpRequest") }} interfaces.</p>
<h2>See also</h2>
<ul> <li><a class="internal" href="/En/Using_XMLHttpRequest" title="En/Using XMLHttpRequest">Using XMLHttpRequest</a></li> <li><a href="/en/AJAX/Getting_Started" title="en/AJAX/Getting_Started">MDC AJAX introduction</a></li> <li><a class="external" href="http://www.peej.co.uk/articles/rich-user-experience.html">XMLHttpRequest - REST and the Rich User Experience</a></li> <li><a class="external" href="http://www.xulplanet.com/references/objref/XMLHttpRequest.html">XULPlanet documentation</a></li> <li><a class="external" href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/xmlsdk/html/xmobjxmlhttprequest.asp">Microsoft documentation</a></li> <li><a class="external" href="http://developer.apple.com/internet/webcontent/xmlhttpreq.html">Apple developers' reference</a></li> <li><a class="external" href="http://jibbering.com/2002/4/httprequest.html">"Using the XMLHttpRequest Object" (jibbering.com)</a></li> <li><a class="external" href="http://www.w3.org/TR/XMLHttpRequest/">The XMLHttpRequest Object: W3C Working Draft</a></li>
</ul>
<p>{{ languages( { "es": "es/XMLHttpRequest", "fr": "fr/XMLHttpRequest", "it": "it/XMLHttpRequest", "ja": "ja/XMLHttpRequest", "ko": "ko/XMLHttpRequest", "pl": "pl/XMLHttpRequest", "zh-cn": "cn/XMLHttpRequest" } ) }}</p>
Revert to this revision