jsonp注意事项

自己测试的:

<?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);

 

< 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 >

 

 

 

 

 

     <! 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 > 

 

php:

<?php  
  
$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

转载于:https://www.cnblogs.com/jami918/p/3593340.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值