<?php
$callback = $_GET['callback' ];
echo "{$callback}({'msg':'this is a jquery jsonp test message!'})";
?>
前端代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>使用JavaScript实现跨域请求</title>
<script type="text/javascript">
var scriptElement = document.createElement("script");
scriptElement.src = "http://zhangzhanyu.nfreehost.com/jsonp.php?callback=callback";
document.head.appendChild(scriptElement);
function callback(jsonString) {
//var data = JSON.parse(jsonString);
alert(jsonString.msg);
}
</script>
</head>
<body>
</body>
</html>