什么样的情况会导致调用此处理程序?我没有找到此方法引发错误的任何实例。
我尝试离线使用设备,我得到 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 开发人员控制台尝试您的代码。
在这里,自己尝试一下:
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/