Skip to content

Instantly share code, notes, and snippets.

@dlabey
Created September 30, 2012 03:15
Show Gist options
  • Save dlabey/3805741 to your computer and use it in GitHub Desktop.
Save dlabey/3805741 to your computer and use it in GitHub Desktop.
JSONP Request/Response Functions
var jsonp = function () {
return {
// JSONP Request
request: function (src, callback) {
try {
var self = this,
script = null;
if (document.getElementById('jsonp') !== null) {
script = document.getElementById('jsonp');
body.removeChild(script);
}
script = document.createElement('script');
script.type = 'text/javascript';
script.id = 'jsonp';
script.src = src;
script.onload = function () {
if ((this.readyState === 'complete' || typeof this.readyState === 'undefined') && typeof callback !== 'undefined') callback(self.responseObject);
}
script.onreadystatechange = function () {
if (typeof callback !== 'undefined') callback(self.responseObject);
}
body.appendChild(script);
} catch (exception) {
error.logError(exception);
}
},
// JSONP Response
response: function (response) {
try {
if (typeof response.error === 'undefined') {
this.responseObject = response;
} else {
handleError(response.error);
}
} catch (exception) {
error.logError(exception);
}
}
}
} ();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment