index.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<script type="text/javascript">
var callBack = function(data){
console.log(data);
}
</script>
<script type="text/javascript" src="./callback.php?callback=callBack"></script>
</body>
</html>
callback.php
<?php
if(isset($_GET['callback'])){
//函数名 -- 也就是index.php 的 callBack
$callback = $_GET['callback'];
//要返回的数据
$arr = array('name' => "Lee", 'city' => "XIAN");
//json编码
$arrJson = json_encode($arr);
//拼接成函数返回
echo $callback ."(".$arrJson.")";
}
?>