Skip to main content

Using Javascript to get value form another site via JSONP

JSONP is JSON with padding, that is, you put a string at the beginning and a pair of parenthesis around it. For example:

//JSON
{"name":"stackoverflow","id":5}
//JSONP
func
({"name":"stackoverflow","id":5});

The result is that you can load the json as a script file. If you
previously set up a function called func, then that function will be
called with one argument, which is the json data, when the script file
is done loading. This is usually used to allow for cross-site AJAX with
JSON data. If you know that example.com is serving json files that look
like the one above, then you can use code like this to retrieve it, even
if you are not on the example.com domain:

function func(json){
  alert
(json.name);
}
var elm = document.createElement("script");
elm
.setAttribute("type", "text/javascript");
elm
.src = "http://example.com/jsonp";
document
.body.appendChild(elm);

Reference

http://stackoverflow.com/questions/2887209/what-are-the-differences-betw...