我正在使用angular并且在为$ q包装XMLHttpRequest时遇到问题。
我也在使用offline.js库来检查用户是否在线。在向fav.ico发出ajax请求后,它将var'status'的状态更改为down \ up。它在调用Offline.check()后进行ajax调用。
Offline.check()返回我要包装到$ q的XMLHttpRequest并承诺在获取后使用结果
我试过了:
$q( Offline.check() ).then(function(response){
console.log(response);
});
但得到错误:$ q:norslvr 没有解析器函数传递给$ Q
offline.js的有趣部分
Offline.check = function() {
Offline.trigger('checking');
return Offline.checks['xhr']();
};
Offline.checks.xhr = function() {
var e, xhr;
xhr = new XMLHttpRequest;
xhr.offline = false;
xhr.open('HEAD', Offline.getOption('checks.xhr.url'), true);
if (xhr.timeout != null) {
xhr.timeout = Offline.getOption('checks.xhr.timeout');
}
checkXHR(xhr, Offline.markUp, Offline.markDown);
try {
xhr.send();
} catch (_error) {
e = _error;
Offline.markDown();
}
return xhr;
};
checkXHR = function(xhr, onUp, onDown) {
var checkStatus, _onerror, _onload, _onreadystatechange, _ontimeout;
checkStatus = function() {
if (xhr.status && xhr.status < 12000) {
return onUp();
} else {
return onDown();
}
};
if (xhr.onprogress === null) {
_onerror = xhr.onerror;
xhr.onerror = function() {
onDown();
return typeof _onerror === "function" ? _onerror.apply(null, arguments) : void 0;
};
_ontimeout = xhr.ontimeout;
xhr.ontimeout = function() {
onDown();
return typeof _ontimeout === "function" ? _ontimeout.apply(null, arguments) : void 0;
};
_onload = xhr.onload;
return xhr.onload = function() {
checkStatus();
return typeof _onload === "function" ? _onload.apply(null, arguments) : void 0;
};
} else {
_onreadystatechange = xhr.onreadystatechange;
return xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
checkStatus();
} else if (xhr.readyState === 0) {
onDown();
}
return typeof _onreadystatechange === "function" ? _onreadystatechange.apply(null, arguments) : void 0;
};
}
};