自己测试的:
<?php
$callback = $_GET["callback"];
$a = $_GET["a"];
$datac= array(
'a'=> $a,
'b'=>'bbcb'
);
$datab= array(
'a'=>'aaabb',
'b'=>'bbbb'
);
echo $callback ? $callback . "(" . json_encode( $datac) . ")" : json_encode( $datab);
$callback = $_GET["callback"];
$a = $_GET["a"];
$datac= array(
'a'=> $a,
'b'=>'bbcb'
);
$datab= array(
'a'=>'aaabb',
'b'=>'bbbb'
);
echo $callback ? $callback . "(" . json_encode( $datac) . ")" : json_encode( $datab);
<
script
type
="text/javascript"
src
="http://upcdn.b0.upaiyun.com/libs/jquery/jquery-1.10.2.min.js"
></
script
>
< input type ='button' value ='ssssssssssssssss' onclick ='test()' >
< script >
function test()
{
$.ajax({
type : " post " ,
url : " http://localhost/test.php " ,
dataType : " jsonp " , // 数据类型为jsonp
// jsonp: "jsonpCallback",//服务端用于接收callback调用的function名的参数
data: { " a " : " aa " , " b " : ' bb ' },
success: function (data){
console.log(data);
},
error: function (){
console.log( ' 1111 ' );
}
});
}
</ script >
< input type ='button' value ='ssssssssssssssss' onclick ='test()' >
< script >
function test()
{
$.ajax({
type : " post " ,
url : " http://localhost/test.php " ,
dataType : " jsonp " , // 数据类型为jsonp
// jsonp: "jsonpCallback",//服务端用于接收callback调用的function名的参数
data: { " a " : " aa " , " b " : ' bb ' },
success: function (data){
console.log(data);
},
error: function (){
console.log( ' 1111 ' );
}
});
}
</ script >
<!
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://mytaobao.com/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 >
</ body >
</ html >
< 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://mytaobao.com/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 >
</ body >
</ html >
php:
<?php
$callback = $_GET["callback"];
$a = array(
'code'=>'CA1998',
'price'=>'6000',
'tickets'=>20,
'func'=> $callback,
);
$result = json_encode( $a);
echo "flightHandler( $result)";
exit;
$callback = $_GET["callback"];
$a = array(
'code'=>'CA1998',
'price'=>'6000',
'tickets'=>20,
'func'=> $callback,
);
$result = json_encode( $a);
echo "flightHandler( $result)";
exit;
参阅 :JSONP跨域的原理解析
http://www.nowamagic.net/librarys/veda/detail/224