Created
February 25, 2013 21:08
-
-
Save oksuz/5033332 to your computer and use it in GitHub Desktop.
Cross Domain Ajax Request [JSONP]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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