2.2より前のすべてのバージョンのjQueryでうまく動作するネイティブのXMLHttpRequest
オブジェクトのかなり簡単な書き直しがあります。私はそれを壊したバージョン2.2で何が変更されたのか把握しようとしています。私はchange log for version 2.2のすべてのコミットを見てきましたが、原因を見つけることができませんでした。jQuery 2.2は私のXHRを改めます
$.ajax
(jQ 2.2以降)で作成されたネイティブAPIからAJAXリクエストが行われた場合、応答は常にundefined
です。ここで
は作業フィドルです:https://plnkr.co/edit/QADOKuxDvAhydrp46Bpk?p=preview
デモフィドルは、jQueryの2.2と同様に、コントラストのコメントでのjQueryの以前のバージョンが含まれています。ここで
は私のXHR再書き込みは、私のコードを破ったことにjQuery 2.2で何が変わった
/**
* 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;
};
...ですか?
私は2016年の終わりに、ここで野生でどのように多くのブラウザを疑問に思う、実際に「ActiveXオブジェクト」を必要としています。 – Pointy
ええ、よくアクティブなサポートx –