问题
I'm working on making an AJAX request from a Firefox extension. I have this code:
function GetMenu(){
var oReq = Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance();
// Setup event handlers - must be set before calling open()
oReq.addEventListener("progress", updateProgress, false);
oReq.addEventListener("load", transferComplete, false);
oReq.addEventListener("error", transferFailed, false);
oReq.addEventListener("abort", transferCanceled, false);
oReq.open('POST', "http://www.foo.bar/", true);
oReq.send('your=data&and=more&stuff=here');
}
function transferFailed(evt) {
Application.console.log("An error occurred while transferring the file.");
Application.console.log(this.responseText);
for(var i in evt)
Application.console.log(i+ ' => '+evt[i]);
}
The request fails because http://www.foo.bar/ does not exist (I assume). My question is, why is there no error message in the evt object passed to transferFailed() that says, "The domain does not exist" or "DNS failure" or something of that nature? None of the event object's properties have any indication of what the problem is, no message, no error code, etc.
Shouldn't there be some sort of indication of what the actual error is?
回答1:
Since you're running with chrome-privileges:
function transferFailed(evt) {
if (this.channel && this.channel.status == Components.results.NS_ERROR_UNKNOWN_HOST) {
alert("DNS error");
}
}
(what @paa said in the comment).
See (you might need to QueryInterface
/instanceof
accordingly):
- nsIRequest
- nsIChannel
- nsIHttpChannel
- nsIHttpChannelInternal
回答2:
Network errors are not propagated to the caller.
status
(and statusText
, though it's whatever the server likes) is about HTTP.
来源:https://stackoverflow.com/questions/18614488/shouldnt-the-error-event-of-xmlhttprequest-have-an-error-message