Skip to content

Instantly share code, notes, and snippets.

@yinyin
Created July 25, 2012 17:55
Show Gist options
  • Save yinyin/3177531 to your computer and use it in GitHub Desktop.
Save yinyin/3177531 to your computer and use it in GitHub Desktop.
JSONP experiment w/ PHP and jQuery
<?php
header('Content-Type: application/json');
$callback = trim($_GET['callback']);
if(!empty($callback))
{ echo $callback, '('; }
echo '{"status": 0, "tstamp": ', time(), '}';
if(!empty($callback))
{ echo ');'; }
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
<script>
<!-- //
$(document).ready(function() {
$.get("http://webserver.local/endpoint.php", function(data) {
$("#d-place").text(data.tstamp);
}, "jsonp");
});
// -->
</script>
</head>
<body>
<p>d = <span id="d-place">.</span></p>
</body>
</html>
<!--
vim: ts=4 sw=4 ai nowrap
-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment