$.getJSON跨域 传递数据 亲测成功
以下是PHP源码:
<?php
$arr = array('title'=>$_GET['title']);
$jsonencode = json_encode($arr);
echo $_GET['callback'] ."(". $jsonencode . ")";
?>
以下是HTML源码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script language="javascript" src="jquery-1.7.js"></script>
<script type="text/javascript" language="javascript">
$(document).ready(function(){
$("button").click(function(){
$.getJSON("http://localhost/index.php?title=2014&callback=?",function(data){
alert('a');
$("p").text(data.title);
})
})
})
</script>
</head>
<body>
<p></p>
<button>获得 JSON 数据</button>
</body>
</html>
//也可以这样写:只是传递数据的形式不同。$.getJSON("http://localhost/index.php?callback=?",{title:"201314"},function(data){
//php源码接收数据依然是:$_GET['title'];