我使用纯 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