Skip to content

Commit

Permalink
Loader now uses XDomainRequest in IE9 to load JSON data to help with …
Browse files Browse the repository at this point in the history
…CORS issues.
  • Loading branch information
photonstorm committed Apr 29, 2014
1 parent 9135b05 commit 75a848f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ Version 2.0.4 - "Mos Shirare" - 29th April 2014
* Tilemap.addTilesetImage will now raise a console.warn if you specify an invalid tileset key and not create the tileset rather than pick the default set.
* Math.smoothstep and Math.smootherstep have been updated to work regardless if a is > or < b (thanks @gre, fix #772)
* Text.updateText now sets the lineCap to `round` to avoid occassional font glitching issues in Chrome.
* Loader now uses XDomainRequest in IE9 to load JSON data to help with CORS issues.

### New Features

Expand Down
56 changes: 50 additions & 6 deletions src/loader/Loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ Phaser.Loader = function (game) {
*/
this._xhr = new XMLHttpRequest();

/**
* @property {XDomainRequest} - An ajax request used specifically by IE9 for CORs loading issues.
* @private
*/
this._ajax = null;

/**
* @property {boolean} isLoading - True if the Loader is in the process of loading the queue.
* @default
Expand Down Expand Up @@ -963,12 +969,50 @@ Phaser.Loader.prototype = {
break;

case 'json':
this._xhr.open("GET", this.baseURL + file.url, true);
this._xhr.responseType = "text";
this._xhr.onload = function () {
return _this.jsonLoadComplete(_this._fileIndex);
};
this._xhr.send();

if (window.XDomainRequest)
{
this._ajax = new window.XDomainRequest();

// XDomainRequest has a few querks. 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);

this._ajax.send();
}
else
{
this._xhr.open("GET", this.baseURL + file.url, true);
this._xhr.responseType = "text";

this._xhr.onload = function () {
return _this.jsonLoadComplete(_this._fileIndex);
};

this._xhr.onerror = function () {
return _this.dataLoadError(_this._fileIndex);
};

this._xhr.send();
}

break;

case 'tilemap':
Expand Down

0 comments on commit 75a848f

Please sign in to comment.