Skip to content

Instantly share code, notes, and snippets.

@thinkphp
Created March 21, 2012 15:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thinkphp/2148789 to your computer and use it in GitHub Desktop.
Save thinkphp/2148789 to your computer and use it in GitHub Desktop.
jsonp v2
function Jsonp(url, callback) {
this.url = url;
this.callback = callback
}
Jsonp.prototype.fetch = function() {
var id = new Date().getTime(),
fn = "callback_" + id,
url = this.url.replace('=?','=Jsonp.' + fn),
that = this,
s = document.createElement('script');
s.type = 'text/javascript'
Jsonp[fn] = this.evalJSON(this.callback,that)
s.src = url
document.getElementsByTagName('head')[0].appendChild(s)
this.s = s
this.fn = fn
}
Jsonp.prototype.evalJSON = function(callback,that) {
return function(data) {
var validjson = false
if(typeof data == 'string') {
try {
validjson = JSON.parse(data)
}catch(e) {
}
} else if(typeof data == 'object') {
validjson = data
} else {
validjson = JSON.parse(JSON.stringify(data));
console.log('response data was not a JSON string')
}
if(validjson) {
callback(validjson)
delete Jsonp[that.fn]
document.getElementsByTagName("head")[0].removeChild(that.s);
} else {
console.log('JSONP call returned invalid JSON or empty JSON')
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment