Jquery getJSON 实现跨域请求 --- callback

Jquery getJSON 实现跨域请求 --- callback

当前域名访问  me.com 文件

<script type="text/javascript" src=" https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script><script>
$.getJSON(' http://localhost/test.php?callback=?',{p1:'11'}, function (msg){ //调用域localhost的test.php(其实两个文件在相同目录下) console.dir(msg);})
</script>
localhost/test.php 文件

<? php  die( $_GET['callback'].'('.json_encode( array('msg'=>1)).')' );?>

 getJSON 核心思想是 执行getJSON  注册一个以当前时间命名的function 然后再服务端执行该函数 (具体源码)

           1、

getJSON:  function( url, data, callback ) {  return jQuery.get(url, data, callback, "json"); },

   2、

复制代码
get:  function ( url, data, callback, type ) {  //  shift arguments if data argument was ommited  if  ( jQuery.isFunction( data ) ) { callback =  data; data null ; }  return  jQuery.ajax({ type: "GET" , url: url, data: data, success: callback, dataType: type });},
复制代码

  3、

复制代码
//  Build temporary JSONP function  if ( s.dataType == "json" && (s.data && s.data.match(jsre) ||  s.url.match(jsre)) ) { jsonp = "jsonp" + jsc++ //  Replace the =? sequence both in the query string and the data  if  ( s.data ) s.data = (s.data + "").replace(jsre, "=" + jsonp + "$1" ); s.url = s.url.replace(jsre, "=" + jsonp + "$1" );  //  We need to make sure  //  that a JSONP style response is executed properly s.dataType = "script" //  Handle JSONP-style loadingwindow[ jsonp ] =  function (tmp){ data =  tmp; success(); complete();  //  Garbage collect window[ jsonp ] =  undefined; trydelete window[ jsonp ]; }  catch (e){}  if  ( head ) head.removeChild( script ); }; }
复制代码
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值