The concept is simple, we pass the url, including the parameter name used to communicate the callback name, and a callback as second argument ... that's it,
Here an example:
<!-- the generic HTML page -->
<script src="JSONP.js"></script>
<script>
this.onload = function () {
var many = 0;
JSONP("test.php?callback", function (a, b, c) {
this.document.body.innerHTML += [
a, b, ++many, c
].join(" ") + "<br />";
});
JSONP("test.php?callback", function (a, b, c) {
this.document.body.innerHTML += [
a, b, ++many, c
].join(" ") + "<br />";
});
};
</script>
And here the testable demo result that should work with every bloody damned browser.
What's on the server? Nothing more than this:
<?php
if (isset($_GET['callback'])) {
header('Content-Type: application/javascript');
exit($_GET['callback'].'.call(window, "Hello", "JSONP", "!!!")');
}
?>
Enjoy ;)
No comments:
Post a Comment