[普通]Jquery getJSON 实现跨域请求 --- callback

作者(passion) 阅读(980次) 评论(0) 分类( web前端)

<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 然后再服务端执行该函数 (具体源码)

    

getJSON: function( url, data, callback ) { return jQuery.get(url, data, callback, "json"); },
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
    });
},
   // 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 loading
            window[ jsonp ] = function(tmp){
                data = tmp;
                success();
                complete();                // Garbage collect
                window[ jsonp ] = undefined;                try{ delete window[ jsonp ]; } catch(e){}                if ( head )
                    head.removeChild( script );
            };
        }


« 上一篇:wifi共享上网(至尊版wifi)
« 下一篇:ASP.NET附加数据库文件的方式,如何发布到IIS7而不导致SQLServer出错
在这里写下您精彩的评论
  • 微信

  • QQ

  • 支付宝

返回首页
返回首页 img
返回顶部~
返回顶部 img