Hi,
I just experienced some behaviour when using an XMLHttpRequest object
from an XPCOM component that I *think* might be a bug. If you create
a XMLHttpRequest object, QI it to nsIDOMEventTarget and add a "load"
and "error" handler, QI it to nsIXMLHttpRequest and open an invalid
URL, when send(null) is called, an NS_ERROR_FILE_NOT_FOUND exception
is thrown. It is my understanding that send shouldn't throw
exceptions for asynchronous connections, but should call the error
handler.
I am I wrong? If so, the documentation for XHR should make this
clearer!
In my sample code, I am catching the exception and invoking the error
handler with a null event (urgh!).
Bruce
----
Sample code:
myObject.prototype.loadURL = function(url, loadListener,
errorListener) {
if (!url) {
throw CE("loadURL: Missing required parameter: url",
ERROR_ILLEGAL_ARGUMENT_EXCEPTION);
}
if (!loadListener) {
throw CE("loadURL: Missing required parameter: loadListener",
ERROR_ILLEGAL_ARGUMENT_EXCEPTION);
}
if (!errorListener) {
throw CE("loadURL: Missing required parameter: errorListener",
ERROR_ILLEGAL_ARGUMENT_EXCEPTION);
}
// Get XMLHTTPRequest object
var xhr = CC("@mozilla.org/xmlextras/xmlhttprequest;
1").createInstance();
// QI to nsIDOMEventTarget to add event listeners
xhr = xhr.QueryInterface(CI("nsIDOMEventTarget"));
// Check and add loadListener
try {
loadListener.QueryInterface(CI("nsIDOMEventListener"));
debug("loadURL: attaching load listener");
xhr.addEventListener("load", loadListener, false);
}
catch (e) {
throw CE("loadURL: loadListener is not valid: ",
ERROR_ILLEGAL_ARGUMENT_EXCEPTION);
}
// Check and add errorListener
try {
errorListener.QueryInterface(CI("nsIDOMEventListener"));
debug("loadURL: attaching error listener");
xhr.addEventListener("error", errorListener, false);
}
catch (e) {
throw CE("loadURL: errorListener is not valid",
ERROR_ILLEGAL_ARGUMENT_EXCEPTION);
}
// QI to nsIXMLHttpRequest to load URL
xhr = xhr.QueryInterface(CI("nsIXMLHttpRequest"));
// Set up request
xhr.open("GET", url, true);
try {
// Send request
xhr.send(null);
}
catch (e) {
debug("loadURL: xhr.send:error: " + e);
errorListener.handleEvent(null);
}
}
function CC(className)
{
return Components.classes[className];
}
function CI(ifaceName)
{
return Components.interfaces[ifaceName];
}
function CR(resultName)
{
return Components.results[resultName];
}
function CE(message, errorcode)
{
return Components.Exception(message, errorcode);
}
_______________________________________________
dev-tech-xpcom mailing list
[EMAIL PROTECTED]
https://lists.mozilla.org/listinfo/dev-tech-xpcom