mozilla

Revision 2224 of Using XMLHttpRequest

  • Revision slug: DOM/XMLHttpRequest/Using_XMLHttpRequest
  • Revision title: Using XMLHttpRequest
  • Revision id: 2224
  • Created:
  • Creator: Sheppy
  • Is current revision? No
  • Comment page created, 108 words added

Revision Content

XMLHttpRequest makes sending HTTP requests very easy.  You simply create an instance of the object, open a URL, and send the request.  The HTTP status of the result, as well as the result's contents, are available in the request object when the transation is completed.

Example: A synchronous request

XMLHttpRequest supports both synchronous and asynchronous communications.  This example demonstrates how to make a simple synchronous request.

Note: As a general rule, you should avoid using synchronous XMLHttpRequests because they block the application's user interface.
var req = new XMLHttpRequest();
req.open('GET', 'http://www.mozilla.org/', false); 
req.send(null);
if(req.status == 200)
  dump(req.responseText);

Revision Source

<p><code>XMLHttpRequest</code> makes sending HTTP requests very easy.  You simply create an instance of the object, open a URL, and send the request.  The HTTP status of the result, as well as the result's contents, are available in the request object when the transation is completed.</p>
<h2>Example: A synchronous request</h2>
<p><code>XMLHttpRequest</code> supports both synchronous and asynchronous communications.  This example demonstrates how to make a simple synchronous request.</p>
<div class="note"><strong>Note:</strong> As a general rule, you should avoid using synchronous <code>XMLHttpRequests</code> because they block the application's user interface.</div>
<pre class="brush: js">var req = new XMLHttpRequest();
req.open('GET', 'http://www.mozilla.org/', false); 
req.send(null);
if(req.status == 200)
  dump(req.responseText);</pre>
Revert to this revision