nsIXMLHttpRequest

  • Revision slug: nsIXMLHttpRequest
  • Revision title: nsIXMLHttpRequest
  • Revision id: 59590
  • Created:
  • Creator: Nickolay
  • Is current revision? No
  • Comment 12 words added

Revision Content

nsIXMLHttpRequest along with {{ interface("nsIJSXMLHttpRequest") }} and {{ interface("nsIXMLHttpRequestEventTarget") }} are Mozilla's implementation details of the DOM XMLHttpRequest object.

Note: If you're a web developer or a Mozilla add-on developer, please refer to the XMLHttpRequest documentation instead.

This page contains documentation, specific to Mozilla application and add-on developers.

The interface definition: http://mxr.mozilla.org/mozilla-centr...ttpRequest.idl

Using event handlers from native code

(Not sure if it's up-to-date)

From native code, the way to set up onload and onerror handlers is a bit different. Here is a comment from Johnny Stenback <jst@netscape.com>:

The mozilla implementation of nsIXMLHttpRequest implements the interface nsIDOMEventTarget and that's how you're supported to add event listeners. Try something like this: nsCOMPtr<nsIDOMEventTarget> target(do_QueryInterface(myxmlhttpreq)); target->AddEventListener(NS_LITERAL_STRING("load"), mylistener, PR_FALSE) where mylistener is your event listener object that implements the interface nsIDOMEventListener. The 'onload', 'onerror', and 'onreadystatechange' attributes moved to nsIJSXMLHttpRequest, but if you're coding in C++ you should avoid using those.

Though actually, if you use addEventListener from C++ weird things will happen too, since the result will depend on what JS happens to be on the stack when you do it....

Conclusion: Do not use event listeners on XMLHttpRequest from C++, unless you're aware of all the security implications. And then think twice about it.

Example code

This is a simple example code for opening a simple HTTP request from a xul application (like a Mozilla extension) without using observers:

 var req = Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance();
 req.open('POST', "http://www.foo.bar:8080/nietzsche.do", true);
 req.send('your=data&and=more&stuff=here');

{{ languages( { "pl": "pl/NsIXMLHttpRequest" } ) }}

Revision Source

<p><code>nsIXMLHttpRequest</code> along with {{ interface("nsIJSXMLHttpRequest") }} and {{ interface("nsIXMLHttpRequestEventTarget") }} are Mozilla's implementation details of the DOM <a href="/en/DOM/XMLHttpRequest" title="en/DOM/XMLHttpRequest">XMLHttpRequest</a> object.</p>
<div class="note"><strong>Note:</strong> If you're a web developer or a Mozilla add-on developer, please refer to the <a href="/en/DOM/XMLHttpRequest" title="en/DOM/XMLHttpRequest">XMLHttpRequest</a> documentation instead.</div>
<p>This page contains documentation, specific to Mozilla application and add-on developers.</p>
<p>The interface definition: <a class=" external" href="http://mxr.mozilla.org/mozilla-central/source/content/base/public/nsIXMLHttpRequest.idl" rel="freelink">http://mxr.mozilla.org/mozilla-centr...ttpRequest.idl</a></p>
<h3 id="Using_event_handlers_from_native_code">Using event handlers from native code</h3>
<p>(Not sure if it's up-to-date)</p>
<p>From native code, the way to set up onload and onerror handlers is a bit different. Here is a comment from Johnny Stenback &lt;<a class=" link-mailto" href="mailto:jst@netscape.com" rel="freelink">jst@netscape.com</a>&gt;:</p>
<blockquote>The mozilla implementation of nsIXMLHttpRequest implements the interface nsIDOMEventTarget and that's how you're supported to add event listeners. Try something like this: nsCOMPtr&lt;nsIDOMEventTarget&gt; target(do_QueryInterface(myxmlhttpreq)); target-&gt;AddEventListener(NS_LITERAL_STRING("load"), mylistener, PR_FALSE) where mylistener is your event listener object that implements the interface nsIDOMEventListener. The 'onload', 'onerror', and 'onreadystatechange' attributes moved to nsIJSXMLHttpRequest, but if you're coding in C++ you should avoid using those.</blockquote>
<p>Though actually, if you use addEventListener from C++ weird things will happen too, since the result will depend on what JS happens to be on the stack when you do it....</p>
<p>Conclusion: Do not use event listeners on XMLHttpRequest from C++, unless you're aware of all the security implications. And then think twice about it.</p>
<h2 id="Example_code" name="Example_code">Example code</h2>
<p>This is a simple example code for opening a simple HTTP request from a xul application (like a Mozilla extension) without using observers:</p>
<pre class="eval"> var req = Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance();
 req.open('POST', "<a class=" external" href="http://www.foo.bar:8080/nietzsche.do" rel="freelink">http://www.foo.bar:8080/nietzsche.do</a>", true);
 req.send('your=data&amp;and=more&amp;stuff=here');
</pre>
<p>{{ languages( { "pl": "pl/NsIXMLHttpRequest" } ) }}</p>
Revert to this revision