Skip to content

Instantly share code, notes, and snippets.

@oranj
Created July 26, 2012 15:17
Show Gist options
  • Save oranj/3182646 to your computer and use it in GitHub Desktop.
Save oranj/3182646 to your computer and use it in GitHub Desktop.
Javascript wrapper for cross domain requests.
var XReq = function(params) {
this.init(params);
this.perform();
}
XReq.prototype = {
'init': function(params) {
var members = {
'url':null,
'error':function(xhr, error, message) {
console.error([error, message]);
},
'success':function(data) {
console.log(["No success handler for url " + that.url]);
},
'dataType':'text',
'async':true,
'data':{},
'type':'GET',
'xd':false
};
this._aborted = false;
for (var i in members) { if (members.hasOwnProperty(i)) {
if (params[i] != undefined) {
this[i] = params[i];
} else if (members[i] == null) {
console.error("Parameter " + i + " is required");
} else {
this[i] = members[i];
}
}}
if (this.url.match(CDN_DOMAIN)) {
this.xd = true;
}
if (! window.XDomainRequest) {
this.xd = false;
}
var that = this;
if (this.xd) {
this.xr = new XDomainRequest();
} else {
this.xr = new XMLHttpRequest();
}
},
'perform':function() {
var query = '', send = '';
var first = true;
for (var i in this.data) { if (this.data.hasOwnProperty(i)) {
query += (first?'':'&') + encodeURIComponent(i) +'='+encodeURIComponent(this.data[i]);
first = false;
}};
if (this.type == 'GET') {
if (query.length) { query = '?' + query; }
this.xr.open(this.type, this.url+query, this.async);
} else {
this.xr.open(this.type, this.url, this.async);
this.xr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
this.xr.setRequestHeader("Content-length", this.data.length);
this.xr.setRequestHeader("Connection", "close");
send = query;
}
var that = this;
this.xr.onerror = function() { that.error(that.xr, 'Could not load URL '+that.url, that.xr.responseText); };
if (this.xd) {
this.xr.onload = function() {
that.on_success();
}
}
this.xr.onreadystatechange = function() {
var state = that.xr.readyState;
if (state == 0 || that._aborted) {
that.on_abort();
} else if (state == 4) /** complete **/ {
try {
that.on_success();
} catch (ex) {
that.error(that.xr, 'error', ex);
}
}
};
try {
this.xr.send(send);
} catch (ex) {
that.error(that.xr, "Error making request ("+that.url+")", that.xr.responseText);
}
},
'on_abort':function() {
this.error(this.xr, 'abort', '');
},
'on_success':function() {
if (this.dataType == 'xml') { this.success(this.xr.responseXML); }
else if (this.dataType == 'json') {
var text = this.xr.responseText;
var json = !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(text.replace(/"(\\.|[^"\\])*"/g, ''))) && eval('(' + text + ')');
this.success(json);
} else {
this.success(this.xr.responseText);
}
},
'abort' : function() { this._aborted = true; this.xr.abort(); }
}
@oranj
Copy link
Author

oranj commented Jul 26, 2012

As it stands, requires jquery to parse the responses.

@oranj
Copy link
Author

oranj commented Jul 27, 2012

added abort handling.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment