xmlhttprequest的错误事件不应该显示错误消息吗? [英] Shouldn't the error event of xmlhttprequest have an error message?

查看:431
本文介绍了xmlhttprequest的错误事件不应该显示错误消息吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从Firefox扩展程序发出AJAX请求.我有以下代码:

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]);
}

请求失败,因为 http://www.foo.bar/不存在(我认为) .我的问题是,为什么传递给transferFailed()的evt对象中没有错误消息,说明该域不存在"或"DNS故障"或类似性质的消息?事件对象的任何属性都无法指示问题所在,没有消息,没有错误代码等.

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?

推荐答案

由于您正在使用chrome-privileges运行,

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");
 }
}

(@ paa在评论中说了什么).

(what @paa said in the comment).

请参阅(您可能需要相应地QueryInterface/instanceof):

See (you might need to QueryInterface/instanceof accordingly):

这篇关于xmlhttprequest的错误事件不应该显示错误消息吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆