Request.CrossDomain
by ScottRippey
HTML
Output: <span id="output"></span>
JavaScript
Request.CrossDomain = new Class({
Extends: Request
,
initialize: function(options) {
this.parent(options);
delete this.headers['X-Requested-With'];
var supportsCORS = 'withCredentials' in this.xhr // Modern browsers use Cross Origin Resource Sharing
, hasXDR = typeof XDomainRequest !== 'undefined'
;
if (!supportsCORS && hasXDR) {
output("Using XDomainRequest");
this.xhr = new XDomainRequest();
this.xhr.onload = this._onLoad.bind(this)
} else {
output("Using XMLHttpRequest")
}
}
, _onLoad: function() {
// Polyfill to make onStateChange successful:
this.xhr.readyState = 4;
this.xhr.status = 200;
this.onStateChange();
}
});
var r = new Request.CrossDomain({
method: 'GET'
, onSuccess: function(result) {
output("Success: ", result);
}
, onFailure: function(xhr) {
output("Error: ", xhr.responseText, xhr.responseXml);
}
});
r.send({
url: '//cors-test.appspot.com/test'
});
function output(msg_) {
var msg = Array.prototype.join.call(arguments, " ");
$('output').appendText(msg + "; ");
}