jsonp.php代码
<?php
$callback = $_GET["callback"];
$a = array(
'code'=>'CA1998',
'price'=>'6000',
'tickets'=>20,
'func'=>$callback,
);
$result = json_encode($a);
echo "flightHandler($result)";
exit; ?>b.php页面代码
<!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>
<title>Untitled Page</title>
<script type="text/javascript" src="jquery-1.7.2.min.js"></script>
<script type="text/javascript">
jQuery(document).ready(function(){
$.ajax({
type: "GET",
async: false,
//url: "http://test/jsonp.php",
url:"http://127.0.0.1:8080/sso/jsonp.php",
dataType: "jsonp",
jsonp: "callback",//传递给请求处理程序或页面的,用以获得jsonp回调函数名的参数名(一般默认为:callback)
jsonpCallback:"flightHandler",//自定义的jsonp回调函数名称,默认为jQuery自动生成的随机函数名,也可以写"?",jQuery会自动为你处理数据
success: function(json){
//alert('您查询到航班信息:票价: ' + json.price + ' 元,余票: ' + json.tickets + ' 张。回调函数名为: '+json.func);
},
error: function(){
alert("fail");
}
});
});
</script>
</head>
<body>
<?php
$a=$_GET['callback'];
echo $a;
?>
</body>
</html>
现在浏览b.php如何用php代码取得price的值6000 ,js能取到值,用的这句alert('您查询到航班信息:票价: ' + json.price + ' 元,余票: ' + json.tickets + ' 张。回调函数名为: '+json.func);
,但php怎么取得他的值呢?
<?php
$callback = $_GET["callback"];
$a = array(
'code'=>'CA1998',
'price'=>'6000',
'tickets'=>20,
'func'=>$callback,
);
$result = json_encode($a);
echo "flightHandler($result)";
exit; ?>b.php页面代码
<!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>
<title>Untitled Page</title>
<script type="text/javascript" src="jquery-1.7.2.min.js"></script>
<script type="text/javascript">
jQuery(document).ready(function(){
$.ajax({
type: "GET",
async: false,
//url: "http://test/jsonp.php",
url:"http://127.0.0.1:8080/sso/jsonp.php",
dataType: "jsonp",
jsonp: "callback",//传递给请求处理程序或页面的,用以获得jsonp回调函数名的参数名(一般默认为:callback)
jsonpCallback:"flightHandler",//自定义的jsonp回调函数名称,默认为jQuery自动生成的随机函数名,也可以写"?",jQuery会自动为你处理数据
success: function(json){
//alert('您查询到航班信息:票价: ' + json.price + ' 元,余票: ' + json.tickets + ' 张。回调函数名为: '+json.func);
},
error: function(){
alert("fail");
}
});
});
</script>
</head>
<body>
<?php
$a=$_GET['callback'];
echo $a;
?>
</body>
</html>
现在浏览b.php如何用php代码取得price的值6000 ,js能取到值,用的这句alert('您查询到航班信息:票价: ' + json.price + ' 元,余票: ' + json.tickets + ' 张。回调函数名为: '+json.func);
,但php怎么取得他的值呢?
js能取到是因为js用ajax提交给后台那个jsonp.php,运行了一遍这个php程序,并且返回了数值.这个ajax的返回结果是晚于b.php运行结束的,换句话说当b.php运行结束后 ajax还没返回值呢,所以当然获取不要