У меня довольно простая перезапись родного XMLHttpRequest
объекта, который отлично работает со всеми версиями jQuery до 2.2. Я пытаюсь выяснить, что изменилось в версии 2.2, которая сломала его. Я просмотрел каждую фиксацию в change log for version 2.2, но не смог найти причину.jQuery 2.2 перерывает мою перезапись XHR
Когда запрос AJAX сделан из родного API, он отлично работает, когда он сделан $.ajax
(от jQ 2.2 или выше), ответ всегда undefined
.
Вот рабочая скрипку: https://plnkr.co/edit/QADOKuxDvAhydrp46Bpk?p=preview
Демо скрипка включает JQuery 2.2, а также предыдущие версии JQuery в комментариях для контраста.
Вот мой XHR переписана ..
/**
* XHR Rewrite
*
* This file attempts to overwrite the XMLHttpRequest object so custom hooks can
* be added, although the current code does not add any hooks or do anything
* special. This is just to test that the XHR function can successfully be
* overridden.
*/
// save the native XHR method to xhrConstructor;
var xhrType = XMLHttpRequest ? "XMLHttpRequest" : "ActiveXObject";
var xhrConstructor = window[xhrType];
// now override the native method
window[xhrType] = function(){
console.log("xhr: constructor");
this._xhr = new (Function.prototype.bind.apply(xhrConstructor, arguments));
this.onreadystatechange = function(){};
this.response = "";
this.readyState = 0;
this.responseText = "";
this.responseType = 'text';
this.responseURL = "";
this.responseXML = null;
this.status = 0;
this.statusText = "";
this.timeout = 0;
this.withCredentials = false;
var _this = this;
this._xhr.onreadystatechange = function() {
_this.getMockProperties();
_this.onreadystatechange.apply(_this._xhr, arguments);
_this.getMockProperties();
};
};
window[xhrType].prototype.UNSENT = 0;
window[xhrType].prototype.OPENED = 1;
window[xhrType].prototype.HEADERS_RECEIVED = 2;
window[xhrType].prototype.LOADING = 3;
window[xhrType].prototype.DONE = 4;
window[xhrType].prototype.getMockProperties = function(){
try{ this.response = this._xhr.response; }catch(e){}
try{ this.readyState = this._xhr.readyState; }catch(e){}
try{ this.responseText = this._xhr.responseText; }catch(e){}
try{ this.responseURL = this._xhr.responseURL; }catch(e){}
try{ this.responseXML = this._xhr.responseXML; }catch(e){}
try{ this.status = this._xhr.status }catch(e){}
try{ this.statusText = this._xhr.statusText; }catch(e){}
};
window[xhrType].prototype.setMockProperties = function(){
try{ this._xhr.responseType = this.responseType; }catch(e){}
try{ this._xhr.timeout = this.timeout; }catch(e){}
try{ this._xhr.withCredentials = this.withCredentials; }catch(e){}
};
window[xhrType].prototype.abort = function(){
console.log("xhr: abort");
this.setMockProperties();
var r = this._xhr.abort.apply(this._xhr, arguments);
this.getMockProperties();
return r;
};
window[xhrType].prototype.getAllResponseHeaders = function(){
console.log("xhr: getAllResponseHeaders");
this.setMockProperties();
var r = this._xhr.getAllResponseHeaders.apply(this._xhr, arguments);
this.getMockProperties();
return r;
};
window[xhrType].prototype.getResponseHeader = function(){
console.log("xhr: getResponseHeader");
this.setMockProperties();
var r = this._xhr.getResponseHeader.apply(this._xhr, arguments);
this.getMockProperties();
return r;
};
window[xhrType].prototype.overrideMimeType = function(){
console.log("xhr: overrideMimeType");
this.setMockProperties();
var r = this._xhr.overrideMimeType.apply(this._xhr, arguments);
this.getMockProperties();
return r;
};
window[xhrType].prototype.setRequestHeader = function(){
console.log("xhr: setRequestHeader");
this.setMockProperties();
var r = this._xhr.setRequestHeader.apply(this._xhr, arguments);
this.getMockProperties();
return r;
};
window[xhrType].prototype.send = function(){
console.log("xhr: send");
this.setMockProperties();
var r = this._xhr.send.apply(this._xhr, arguments);
this.getMockProperties();
return r;
};
window[xhrType].prototype.open = function(){
console.log("xhr: open");
this.setMockProperties();
var r = this._xhr.open.apply(this._xhr, arguments);
this.getMockProperties();
return r;
};
Что изменилось в JQuery 2.2, которая сломала мой код?
Интересно, сколько браузеров в дикой природе здесь в конце 2016 года на самом деле требуют «ActiveXObject». – Pointy
Да, хорошо, часто бывает безумным при отладке, например, поддержка активной x –