xmlHttpRequest.onerror处理程序用例 [英] xmlHttpRequest.onerror handler use case

查看:226
本文介绍了xmlHttpRequest.onerror处理程序用例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哪种情况会导致调用此处理程序?我找不到这个方法抛出错误的任何实例。



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



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

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

xmlhttp.open(method,url,true);
xmlhttp.onerror = function(){
console.log(**交易期间发生错误);
};
xmlhttp.send();

来自:



在这里,亲自尝试:



  var xmlhttp = new XMLHttpRequest(),method ='GET',url ='https://developer.mozilla.org/';xmlhttp.open(method,lll,true); xmlhttp.onerror = function(){console.log( **交易期间发生错误);}; xmlhttp.send();  



在处理任何基于网络的IO时,可能会发生各种各样的事情。跨源请求只有一个。如果服务器处于脱机状态,DNS查找失败,您和服务器之间的关键故障点之间的路由器出现故障怎么办?


What sort of situations could cause this handler to be called? I'm not finding any instance where this method throws an error.

I tried with the device offline, I get xmlHttpRequest.status = 0 but no error.

Question is what sort of situations can I create in order to test functionality of this handler.

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();

From: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequestEventTarget/onerror

解决方案

Your question is the perfect example. Just try your code from your web developer console while on this very page.

Here, try it yourself:

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();

When dealing with any network based IO all kinds of things could happen. Cross-Origin requests are only one. What if the server is offline, DNS lookup fails, a router between you and the server that is critical point of failure goes down?

这篇关于xmlHttpRequest.onerror处理程序用例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆