xmlHttpRequest.onerror 处理程序用例
xmlHttpRequest.onerror handler use case
什么样的情况会导致调用此处理程序?我没有找到此方法引发错误的任何实例。
我尝试离线使用设备,我收到 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 查找失败,你和服务器之间的路由器故障怎么办?
由于 XHR 调用是针对服务器响应的,因此当服务器出现错误时,onerror 就会发挥作用。将您的客户端更改为离线不会模拟服务器错误。
假设服务器资源被移动并且服务器响应 404 错误?服务器超时怎么办?如果请求本身格式错误并导致服务器抛出错误怎么办?
什么样的情况会导致调用此处理程序?我没有找到此方法引发错误的任何实例。
我尝试离线使用设备,我收到 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 查找失败,你和服务器之间的路由器故障怎么办?
由于 XHR 调用是针对服务器响应的,因此当服务器出现错误时,onerror 就会发挥作用。将您的客户端更改为离线不会模拟服务器错误。
假设服务器资源被移动并且服务器响应 404 错误?服务器超时怎么办?如果请求本身格式错误并导致服务器抛出错误怎么办?