Last active
August 29, 2015 14:07
-
-
Save red2678/8aa81247b00a924f3276 to your computer and use it in GitHub Desktop.
JSONP Example
This file contains hidden or 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
// Firstly you need access to the JSON file to wrap it in a “callback function”. | |
//In this case I have called it “jsonCallback”. This will be the function called by the AJAX request. | |
jsonCallback( | |
{ | |
"sites": | |
[ | |
{ | |
"siteName": "JQUERY4U", | |
"domainName": "http://www.jquery4u.com", | |
"description": "#1 jQuery Blog for your Daily News, Plugins, Tuts/Tips & Code Snippets." | |
}, | |
{ | |
"siteName": "BLOGOOLA", | |
"domainName": "http://www.blogoola.com", | |
"description": "Expose your blog to millions and increase your audience." | |
}, | |
{ | |
"siteName": "PHPSCRIPTS4U", | |
"domainName": "http://www.phpscripts4u.com", | |
"description": "The Blog of Enthusiastic PHP Scripters" | |
} | |
] | |
} | |
); | |
//Now we can request the JSON via AJAX using JSONP and the callback function we created around the JSON content. | |
var url = 'http://www.jquery4u.com/scripts/jquery4u-sites.json?callback=?'; | |
$.ajax({ | |
type: 'GET', | |
url: url, | |
async: false, | |
jsonpCallback: 'jsonCallback', | |
contentType: "application/json", | |
dataType: 'jsonp' | |
}) | |
.done(function(jsonObj) { | |
console.dir(jsonObj.sites); | |
}) | |
.fail(function(errString){ | |
console.dir(errString); | |
}) | |
.always(funciton(){ | |
console.log("Yay! All done!"); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment