Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support the XDomainRequest object of IE8 and IE9. Those browsers are … #147

Open
wants to merge 1 commit into
base: gh-pages
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 48 additions & 25 deletions lib/basket.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,32 +46,55 @@

var getUrl = function( url ) {
var promise = new RSVP.Promise( function( resolve, reject ){

var xhr = new XMLHttpRequest();
xhr.open( 'GET', url );

xhr.onreadystatechange = function() {
if ( xhr.readyState === 4 ) {
if ( ( xhr.status === 200 ) ||
( ( xhr.status === 0 ) && xhr.responseText ) ) {
resolve( {
content: xhr.responseText,
type: xhr.getResponseHeader('content-type')
} );
} else {
reject( new Error( xhr.statusText ) );
var xhr;

if (typeof XDomainRequest === 'undefined') {
xhr = new XMLHttpRequest();

xhr.onreadystatechange = function() {
if ( xhr.readyState === 4 ) {
if ( ( xhr.status === 200 ) ||
( ( xhr.status === 0 ) && xhr.responseText ) ) {
resolve( {
content: xhr.responseText,
type: xhr.getResponseHeader('content-type')
} );
} else {
reject( new Error( xhr.statusText ) );
}
}
}
};

// By default XHRs never timeout, and even Chrome doesn't implement the
// spec for xhr.timeout. So we do it ourselves.
setTimeout( function () {
if( xhr.readyState < 4 ) {
xhr.abort();
}
}, basket.timeout );
};

// By default XHRs never timeout, and even Chrome doesn't implement the
// spec for xhr.timeout. So we do it ourselves.
setTimeout( function () {
if( xhr.readyState < 4 ) {
xhr.abort();
}
}, basket.timeout );
}
else {
/* XDomainRequest supports CORS ajax calls on IE8 and IE9 but with the following gotchas
*
* 1. The server protocol must be the same as the calling page protocol.
* 2. Only “text/plain” is supported for the request’s “Content Type header
* 3. No custom headers can be added to the request
*/

xhr = new XDomainRequest();
xhr.timeout = basket.timeout;
xhr.onload = function() {
resolve( {
content: xhr.responseText,
type: xhr.contentType
} );
};
xhr.onerror = function(error) {
reject( new Error( error ) );
};
}

xhr.open( 'GET', url );
xhr.send();
});

Expand All @@ -85,7 +108,7 @@
if (!obj.skipCache) {
addLocalStorage( obj.key , storeObj );
}

return storeObj;
});
};
Expand Down