无法在纯js中处理net::ERR_CONNECTION_REFUSED
cannot handle net::ERR_CONNECTION_REFUSED in pure js
我使用纯js(不带JQuery)向服务器发送XMLHttpRequest
请求。
var _xhr = new XMLHttpRequest();
_xhr.open(type, url);
_xhr.setRequestHeader('Content-Type', 'application/json');
_xhr.responseType = 'json';
_xhr.onload = function () {
if (_xhr.status === 200) {
var _json = _xhr.response;
if (typeof _json == 'string')
_json = JSON.parse(_json);
success(_json);
} else {
error(_xhr);
}
};
_xhr.onerror = function(){
console.log('fail');
};
try {
_xhr.send(_to_send);
} catch (e){
options.error(_xhr);
}
当我发送请求时,它失败了(这是可以的),我得到了一个错误,但我无法处理它。xhr.onerror
将消息打印到控制台,但我也得到了OPTIONS url net::ERR_CONNECTION_REFUSED
。如何在控制台中避免此消息?
此外,我使用window.onerror
来处理所有错误,但我无法处理此OPTIONS url net::ERR_CONNECTION_REFUSED
这对我有效:
// Watch for net::ERR_CONNECTION_REFUSED and other oddities.
_xhr.onreadystatechange = function() {
if (_xhr.readyState == 4 && _xhr.status == 0) {
console.log('OH NO!');
}
};
这不仅限于net::ERR_CONNECTION_REFUSED,因为其他网络错误(例如net::ERR_network_CHANGED、net::ERR_ADDRESS_UNREACHABLE和net::ERR_TIMED_OUT)可能会以这种方式"捕获"。我无法在_xhr对象中的任何位置找到这些错误消息,因此通过编程确定具体发生了哪个网络错误是不可靠的。
控制台中显示的错误是无法避免的。就像您请求一个不存在的文件一样,无论您是否处理了错误,您都会在控制台中得到一个404
。
相关文章:
- 类型错误'未定义'不是对象(正在评估'navigator.connection.type'
- 可以't从peerConnection Connection播放视频
- 安装节点6后出错,npm ERR!无效版本:1.0.5beta
- system.err风格的控制台以javascript打印
- AngularJS:使用ng-show/hide动态维护下拉列表的3种状态(InProgress、Success、Err
- npm ERR!安装无法't在mac上读取依赖项
- RTCMultiConnection exception on connection.addStream()
- node-mysql connection.query() returns undefined
- Javascript:connection.query函数正在破坏我的for循环
- 通过带有err,value签名的回调创建promise
- 访问套接字中的其他套接字.在socket.io中打开('connection')
- WebSockets connection.send not function error
- 在 Mocha 中,返回将被拒绝的承诺与调用 done(err) 的效果不同
- Signalr connection.start.done not firing
- SignalR $.connection is undefined
- 节点服务器响应错误: process.nextTick(function(){throw err;});.
- socket.io 中的第二个 io.connection() 不会触发连接事件
- Nodejs Numtel Mysql over a SSH connection
- VMware webMKS(WMKS) console connection url?
- res://ieframe.dll/acr-err 在 Java 6 上的 IE 11 //Windows 8/8.1/