<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JSONP 实例</title>
</head>
<body>
<script type="text/javascript">
function callbackFunction(result, methodName)
{
var html = '<ul>';
for(var i = 0; i < result.length; i++)
{
html += '<li>' + result[i] + '</li>';
}
html += '</ul>';
document.body.innerHTML += html;
}
</script>
<script type="text/javascript" src="http://localhost:80/jsonp.php?callBack=callbackFunction&a=1"></script>
<script type="text/javascript" src="http://localhost:80/jsonp.php?callBack=callbackFunction&a=2"></script>
<script type="text/javascript" src="http://localhost:80/jsonp.php?callBack=callbackFunction&a=3"></script>
</body>
</html>
<?php
header('Content-type: application/json');
//获取回调函数名
$callBack = htmlspecialchars($_REQUEST ['callBack']);
//json数据
$arr = '["hello","hi"]';
//输出jsonp格式的数据
echo $callBack . "(" . $arr . ")";
?>