javascript - xmlHttpRequest.onerror 处理程序用例

标签 javascript ajax xmlhttprequest onerror

什么样的情况会导致调用此处理程序?我没有找到此方法引发错误的任何实例。

我尝试离线使用设备,我得到 xmlHttpRequest.status = 0 但没有错误。

问题是我可以创建什么样的情况来测试此处理程序的功能。

var xmlhttp = new XMLHttpRequest(),
  method = 'GET',
  url = 'https://developer.mozilla.org/';

xmlhttp.open(method, url, true);
xmlhttp.onerror = function () {
  console.log("** An error occurred during the transaction");
};
xmlhttp.send();

发件人:https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequestEventTarget/onerror

最佳答案

您的问题就是一个很好的例子。只需在此页面上从您的 Web 开发人员控制台尝试您的代码。

enter image description here

在这里,自己尝试一下:

var xmlhttp = new XMLHttpRequest(),
  method = 'GET',
  url = 'https://developer.mozilla.org/';

xmlhttp.open(method, url, true);
xmlhttp.onerror = function () {
  console.log("** An error occurred during the transaction");
};
xmlhttp.send();

在处理任何基于网络的 IO 时,可能会发生各种各样的事情。跨源请求只是其中之一。如果服务器离线、DNS 查找失败、您和服务器之间的路由器故障怎么办?

关于javascript - xmlHttpRequest.onerror 处理程序用例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45067892/

相关文章:

javascript - SocketIO volatile 发射到命名空间

javascript - jQuery 绑定(bind) - 如何将此 Ajax 调用的结果绑定(bind)到文档?

javascript - 如何每X秒自动刷新页面标题

jquery - 通过发送ajax请求下载Excel文件

javascript - Phonegap xmlhttprequest 不返回 xml 数据

ios - 来源 http ://localhost:8100 is not allowed by Access-Control-Allow-Origin in ionic

c# - 使用 jquery 创建一个列表并将其发布到 asp.net mvc

Javascript 回调函数 - Node.js

javascript - 使用 Plupload 和 Symfony3 抛出 ValidationException 时无响应

javascript - 是否可以测试 XmlHttpRequest 是否会命中缓存而不是服务器?