use XDomainRequest in Loader.xhrLoad

This commit is contained in:
Mark Wecke 2015-02-06 13:52:24 +01:00
parent 4d8f9e8723
commit 3a70677a05

View file

@ -146,7 +146,7 @@ Phaser.Loader = function (game) {
* @property {XDomainRequest} - An ajax request used specifically by IE9 for CORs loading issues.
* @private
*/
this._ajax = null;
this._xdr = null;
};
@ -1296,42 +1296,7 @@ Phaser.Loader.prototype = {
case 'json':
if (this.useXDomainRequest && window.XDomainRequest)
{
this._ajax = new window.XDomainRequest();
// XDomainRequest has a few quirks. Occasionally it will abort requests
// A way to avoid this is to make sure ALL callbacks are set even if not used
// More info here: http://stackoverflow.com/questions/15786966/xdomainrequest-aborts-post-on-ie-9
this._ajax.timeout = 3000;
this._ajax.onerror = function () {
return _this.dataLoadError(_this._fileIndex);
};
this._ajax.ontimeout = function () {
return _this.dataLoadError(_this._fileIndex);
};
this._ajax.onprogress = function() {};
this._ajax.onload = function(){
return _this.jsonLoadComplete(_this._fileIndex);
};
this._ajax.open('GET', this.baseURL + file.url, true);
// Note: The xdr.send() call is wrapped in a timeout to prevent an issue with the interface where some requests are lost
// if multiple XDomainRequests are being sent at the same time.
setTimeout(function () {
_this._ajax.send();
}, 0);
}
else
{
this.xhrLoad(this._fileIndex, this.baseURL + file.url, 'text', 'jsonLoadComplete', 'dataLoadError');
}
this.xhrLoad(this._fileIndex, this.baseURL + file.url, 'text', 'jsonLoadComplete', 'dataLoadError');
break;
case 'xml':
@ -1380,11 +1345,40 @@ Phaser.Loader.prototype = {
* @param {string} onerror - A String of the name of the local function to be called on a file load error.
*/
xhrLoad: function (index, url, type, onload, onerror) {
var _this = this;
if(this.useXDomainRequest && window.XDomainRequest) {
if(!this._xdr) {
this._xdr = new XDomainRequest();
this._xdr.timeout = 3000;
this._xdr.onprogress = function () {};
}
this._xdr.onload = function () {
_this[onload](index);
};
this._xdr.onerror = function () {
_this[onerror](index);
};
this._xdr.ontimeout = function () {
_this[onerror](index);
};
this._xdr.open("GET", url, true);
this._xdr.responseType = type;
setTimeout(function () {
_this._xdr.send();
});
return;
}
this._xhr.open("GET", url, true);
this._xhr.responseType = type;
var _this = this;
this._xhr.responseType = type;
this._xhr.onload = function () {
return _this[onload](index);
@ -1474,7 +1468,6 @@ Phaser.Loader.prototype = {
file.loaded = true;
var loadNext = true;
var _this = this;
switch (file.type)
{
@ -1525,41 +1518,7 @@ Phaser.Loader.prototype = {
// Load the XML before carrying on with the next file
loadNext = false;
if (this.useXDomainRequest && window.XDomainRequest)
{
this._ajax = new window.XDomainRequest();
// XDomainRequest has a few quirks. Occasionally it will abort requests
// A way to avoid this is to make sure ALL callbacks are set even if not used
// More info here: http://stackoverflow.com/questions/15786966/xdomainrequest-aborts-post-on-ie-9
this._ajax.timeout = 3000;
this._ajax.onerror = function () {
return _this.dataLoadError(_this._fileIndex);
};
this._ajax.ontimeout = function () {
return _this.dataLoadError(_this._fileIndex);
};
this._ajax.onprogress = function() {};
this._ajax.onload = function(){
return _this.xmlLoadComplete(_this._fileIndex);
};
this._ajax.open('GET', this.baseURL + file.xmlURL, true);
// Note: The xdr.send() call is wrapped in a timeout to prevent an issue with the interface where some requests are lost
// if multiple XDomainRequests are being sent at the same time.
setTimeout(function () {
_this._ajax.send();
}, 0);
}
else
{
this.xhrLoad(this._fileIndex, this.baseURL + file.xmlURL, 'text', 'xmlLoadComplete', 'dataLoadError');
}
this.xhrLoad(this._fileIndex, this.baseURL + file.xmlURL, 'text', 'xmlLoadComplete', 'dataLoadError');
}
break;
@ -1655,9 +1614,9 @@ Phaser.Loader.prototype = {
var file = this._fileList[index];
if (this._ajax && this._ajax.responseText)
if (this._xdr && this._xdr.responseText)
{
var data = JSON.parse(this._ajax.responseText);
var data = JSON.parse(this._xdr.responseText);
}
else
{
@ -1736,13 +1695,13 @@ Phaser.Loader.prototype = {
xmlLoadComplete: function (index) {
var data;
if (this._ajax && this._ajax.responseText) {
if (this._ajax.contentType !== '' && this._ajax.contentType !== 'text/plain')
if (this._xdr && this._xdr.responseText) {
if (this._xdr.contentType !== '' && this._xdr.contentType !== 'text/plain')
{
console.warn('Invalid XML Response Type', this._fileList[index]);
console.warn(this._ajax);
console.warn(this._xdr);
}
data = this._ajax.responseText;
data = this._xdr.responseText;
}
else if (this._xhr && this._xhr.responseText) {
if (this._xhr.responseType !== '' && this._xhr.responseType !== 'text')