https://github.com/thinkphp/jsonp
A standalone lightweight JSONP JavaScript Library without dependencies that creates a JSON request using script tag injection and handles callbacks for you.
https://github.com/thinkphp/jsonp
Last synced: 3 months ago
JSON representation
A standalone lightweight JSONP JavaScript Library without dependencies that creates a JSON request using script tag injection and handles callbacks for you.
- Host: GitHub
- URL: https://github.com/thinkphp/jsonp
- Owner: thinkphp
- Created: 2012-04-07T10:39:26.000Z (almost 14 years ago)
- Default Branch: master
- Last Pushed: 2012-04-11T17:07:04.000Z (almost 14 years ago)
- Last Synced: 2024-04-14T14:54:26.539Z (almost 2 years ago)
- Language: JavaScript
- Homepage: http://thinkphp.ro/apps/js-hacks/JSONP/
- Size: 102 KB
- Stars: 3
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
JSONP
=====
JSONP or JSON with padding is a complement to the base JavaScript Object Notation JSON data format. It provides a method to
request data from a server in a different domain. CORS can be used as a modern alternative to the JSONP pattern.
Syntax:
new Jsonp(url, callback).fetch()
OR
new jsonp.fetch(url, callback)
OR
JSONP.get(url, params, callback)
OR
JSONP.init({callbackName: 'jsoncallback'})
JSONP.get(url, params, callback);
How to use:
-----------
#Example-1
var username = 'yelf',
endpoint = 'http://query.yahooapis.com/v1/public/yql?q=',
yql = 'use "http://thinkphp.ro/apps/lastfm/YQL-open-data-table/recentlastfm.xml" as lastfm;select * from lastfm where username="'+ username +'" and api_key="2993c6e15c91a2890c2f11fa95673067"',
url = endpoint + encodeURIComponent(yql) + '&format=json&callback=?'
function callback(data) {
document.getElementById('result').innerHTML = data.query.results.result;
}
new Jsonp(url,callback).fetch();
#Example-2
var tpl = "
{from_user} {text}{created_at}
urltwitter = 'http://search.twitter.com/search.json?q=mootools&rpp=5&callback=?';
new Jsonp(urltwitter, function(data){
var result = data.results,
out = '
- '
for(var i in result) {
out += template(tpl,result[i])
}
out += '
document.getElementById('result-twitter').innerHTML = out
}).fetch();
#example-3
var url = 'http://search.twitter.com/search.json';
JSONP.get(url, {q: 'mootools', rpp: 10}, function(data){
//do stuff with data
})
#example-4
var urltwitter = 'http://search.twitter.com/search.json?q=mootools&rpp=5&callback=?'
jsonp.fetch(urltwitter, function(data){
var result = data.results,
out = '
- '
for(var i in result) {
out += template(tpl,result[i])
}
out += '
document.getElementById('result-twitter').innerHTML = out
})
#example-5
//Get flickr photos search
var tpl = "

url = 'http://api.flickr.com/services/rest/';
//Set Callback Name
JSONP.init({callbackName: 'jsoncallback'})
JSONP.get(url,
{api_key: 'e407090ddb7d7c7c36e0a0474289ec74',
per_page: 20,
page: 1,
text: 'beach kudos',
has_geo: true,
method: 'flickr.photos.search',
format: 'json'},
function(data) {
var photos = data.photos.photo,
n = photos.length,
out = '
- ';
for(var i=0; i';
$('container').innerHTML = out;
})
//we can make multiple requests
JSONP.get(url,
{api_key: 'e407090ddb7d7c7c36e0a0474289ec74',
per_page: 20,
page: 1,
text: 'san francisco',
has_geo: true,
method: 'flickr.photos.search',
format: 'json'},
function(data) {
var photos = data.photos.photo,
n = photos.length,
out = '
- ';
for(var i=0; i';
$('container').innerHTML = out;
})
**Happy Requesting!**