javascript – 如何捕获涉及XmlHttpRequest的错误?

当我通过
XMLHttpRequest将数据发送到服务器时,我希望在TRY {} CATCH(){}的帮助下捕获所有错误.

如何收到所有错误,例如net :: ERR_INTERNET_DISCONNECTED等?

最佳答案 参考这个,

function createXMLHttpRequestObject()
{
  // xmlHttp will store the reference to the XMLHttpRequest object
  var xmlHttp;
  // try to instantiate the native XMLHttpRequest object
  try
  {
    // create an XMLHttpRequest object
    xmlHttp = new XMLHttpRequest();
  }
  catch(e)
  {
        try
    {
      xmlHttp = new ActiveXObject("Microsoft.XMLHttp");
    }
    catch(e) { }
  }
  // return the created object or display an error message
  if (!xmlHttp)
    alert("Error creating the XMLHttpRequest object.");
  else 
    return xmlHttp;
}
点赞