【问题标题】:Shouldn't the error event of xmlhttprequest have an error message?xmlhttprequest的error事件不应该有错误信息吗?
【发布时间】:2013-09-04 12:57:07
【问题描述】:

我正在处理来自 Firefox 扩展的 AJAX 请求。我有这个代码:

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 故障”或类似的东西?事件对象的所有属性都没有任何迹象表明问题是什么,没有消息,没有错误代码等。

不应该有某种迹象表明实际错误是什么吗?

【问题讨论】:

    标签: javascript ajax xmlhttprequest firefox-addon


    【解决方案1】:

    由于您使用 chrome-privileges 运行:

    function transferFailed(evt) {
     if (this.channel && this.channel.status == Components.results.NS_ERROR_UNKNOWN_HOST) {
       alert("DNS error");
     }
    }
    

    (@paa 在评论中所说的)。

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

    【讨论】:

      【解决方案2】:

      网络错误不会传播给调用者。

      status(和statusText,尽管它是服务器喜欢的任何东西)是关于 HTTP 的。

      【讨论】:

      • 你可以试试 mozilla-only channel 属性。如果它在错误处理程序中有效,那么在您的示例中this.channel.status 将返回Components.results.NS_ERROR_UNKNOWN_HOST
      猜你喜欢
      • 2017-12-14
      • 1970-01-01
      • 2021-11-24
      • 2012-06-02
      • 1970-01-01
      • 2010-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多