我正在创建包含XMLHttpRequest的小函数。我需要实现abort,error和timeout处理程序,但是当我调用request.abort()时;它被处理为超时或错误,我需要以某种方式区分这些条件,我该怎么做?
Request = (function () {
var call;
function Request(args) {
if (!(this instanceof Request)) {
return new Request(args);
}
call = new XMLHttpRequest();
args.async = args.async || true;
args.method = args.method || 'GET';
call.open(args.method, args.url, args.async);
if (args.async) {
call.timeout = args.timeout || call.timeout;
}
call.onreadystatechange = function () {
if (call.readyState === 4 && call.status === 200) {
return args.success && args.success(call.responseText, call);
}
//I tried with "readyState === 0" but result was the same
if (call.readyState === 4 && call.status !== 200) {
return args.error && args.error(call);
//here error is callback that passed call for furher handling
//next I should check "call" object to see if was aborted or true error or timeout
}
};
try {
return call.send();
} catch (error) {
return args.error(call.responseText, call);
}
}
Request.prototype.abort = function () {
return call.abort();
};
return Request;
}());