首页 > 代码库 > jsonp 使用示例

jsonp 使用示例

客户端:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
<script src="http://img1.tuniucdn.com/j/201406191714/3rd/jquery-1.7.2.min.js"></script>
<!-- <script src="http://www.mamicode.com/js/require.js" data-main="../js/main"></script> -->
<script type="text/javascript">

function cjCallback(result){
console.log(result.Name);
}
$.get(‘http://localhost/html/index.php?jsonp_callback=cjCallback‘,function(){},"jsonp");


</script>


</body>
</html>

 

服务器端:

<?php

header(‘Content-Type:text/json;charset=utf-8‘);
$str = array
(
‘Name‘=>‘xiaolou‘,
‘Age‘=>20
);

$jsonencode = json_encode($str);
echo $_GET["jsonp_callback"]."(".$jsonencode.")";

?>