Skip to content

Instantly share code, notes, and snippets.

@thinkphp
Created March 21, 2012 10:03
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/2145907 to your computer and use it in GitHub Desktop.
Save thinkphp/2145907 to your computer and use it in GitHub Desktop.
jsonp
var jsonp = {
counter: 0,
fetch: function(url, callback) {
var fn = "jsoncallback_" + this.counter++;
window[fn] = this.evalJsonp(callback);
url = url.replace('=?','=' + fn)
var s = document.createElement('script');
s.type = 'text/javascript';
s.src = url;
document.getElementsByTagName('head')[0].appendChild(s);
},
evalJsonp: function(callback) {
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)
} 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