Created
September 30, 2012 03:15
-
-
Save dlabey/3805741 to your computer and use it in GitHub Desktop.
JSONP Request/Response Functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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