Skip to content

Instantly share code, notes, and snippets.

@oksuz
Created February 25, 2013 21:08
Show Gist options
  • Save oksuz/5033332 to your computer and use it in GitHub Desktop.
Save oksuz/5033332 to your computer and use it in GitHub Desktop.
Cross Domain Ajax Request [JSONP]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="">
<head>
<meta httpd-equiv="content-type" content="text/html;charset=UTF-8" />
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script>
$(document).ready(function() {
$("#button").click(function() {
var surl = "http://jsonp.tld/json.php";
var id = $(this).attr("id");
$.ajax({
url: surl,
data: {id: id},
dataType: "jsonp",
jsonp : "callback",
jsonpCallback: "jsonpcallback"
});
});
});
function jsonpcallback(rtndata) {
alert(rtndata.message)
}
</script>
</head>
<body>
<input type="button" name="button" id="button" value="Click Me !" />
<input type="text" id="result">
</body>
</html>
<?php
header("content-type: application/json");
$rtnjsonobj = new stdClass();
$rtnjsonobj->message = "Cross Domain Request Succeed";
echo $_GET['callback'] . '('. json_encode($rtnjsonobj) . ')';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment