JSONP borns to provides cross-domain resources interoperability....
It simply works by creating and adding a <script> element tag in runtime...
Here is a simple implementation.....
function methodA () {
//your code.....
//and call to remote(and with other domain) url.....
jsonp_request('http://another_site:1234/YourServletOrOtherService');
}
//
//http://another_site:1234/YourServletOrOtherService
//will returns a string similar to this...
// methodB([{"result":"pippo"}])
//
//methodB
// stay for the next js method that will be called....
//
//{"result":"pippo"}
// stay for json data structure to send as returns data
//
//
function jsonp_request (url) {
var head = document.getElementsByTagName("head")[0];
var script = document.createElement("SCRIPT");
script.type = "text/javascript";
script.src = url ;
head.appendChild(script);
}
function methodB (data) {
var current_result = data[0].result;
alert (current_result) ;
//elaborate your data json structure..... a single value or an array of values.....
}
Reference url....: http://json-p.org
Bye....
No comments:
Post a Comment