$.getJSON()跨域之原理

/*可以跨域请求数据*/

其原理是对<script>装载的服务端数据用全局函数(callback)操作

首先写个简单示例:(jsonpCallback可动态创建注册成全局函数)

<script type="text/javascript">  
var m = Math.random();
var jsonpCallback = new Function("result", "alert(result.data);");
</script>  
<script type="text/javascript" src="http://localhost/json.php?jsonp=jsonpCallback"></script>  

 

服务端:

<?php
echo "jsonpCallback({data:'json data'})"; 

 

会弹出:json data

 

 


$.get(url, params, callback)

与$.post请求方法一样,只是请求类型不同

返回的是字符格式,可以用 $.evalJSON()方法进行转换格式

然后对JSON对象进行操作


$.getJSON(url, params, callback)

返回JSON对象,跨域示例如下:



  1. <?php

  2. function getdata()

  3. {

  4.         $.getJSON(

  5.         "http://www.test.com/payment/payment/paytest?callback=?",

  6.         {id:1,name:"name"},

  7.         function(jsondata){

  8.                 alert(jsondata.json);

  9.         });

  10. }


  11. 翻开jquery.的源码,一步步找:


  12. getJSON: function( url, data, callback ) {

  13.         return jQuery.get(url, data, callback, "json");

  14. }


  15. 再找JQuery.get


  16. get: function( url, data, callback, type ) {

  17.         // shift arguments if data argument was omited


  18.         if ( jQuery.isFunction( data ) ) {

  19.                 type = type || callback;

  20.                 callback = data;

  21.                 data = null;

  22.         }


  23.         return jQuery.ajax({

  24.                 type: "GET",

  25.                 url: url,

  26.                 data: data,

  27.                 success: callback,

  28.                 dataType: type

  29.         });

  30. }


  31. 再找 jQuery.ajax


  32. jQuery.ajax({

  33.         url: url,

  34.         type: type,

  35.         dataType: "html",

  36.         data: params,

  37.         complete: function( res, status ) {

  38.                 // If successful, inject the HTML into all the matched elements

  39.                 if ( status === "success" || status === "notmodified" ) {

  40.                         // See if a selector was specified

  41.                         self.html( selector ?

  42.                         // Create a dummy div to hold the results

  43.                         jQuery("<div />")

  44.                         // inject the contents of the document in, removing the scripts

  45.                         // to avoid any 'Permission Denied' errors in IE

  46.                         .append(res.responseText.replace(rscript, ""))


  47.                         // Locate the specified elements

  48.                         .find(selector) :


  49.                         // If not, just inject the full result

  50.                         res.responseText );

  51.                 }


  52.                 if ( callback ) {

  53.                         self.each( callback, [res.responseText, status, res] );

  54.                 }

  55.         }

  56. });


  57. return this;

  58. }


  59. 再找ajax方法,揭开秘密要来了:


  60. 由于太多,帖开头部分,有兴趣的同学自己去看下


  61. ajax: function( origSettings ) {

  62.         var s = jQuery.extend(true, {}, jQuery.ajaxSettings, origSettings);


  63.         var jsonp, status, data,

  64.         callbackContext = origSettings && origSettings.context || s,

  65.         type = s.type.toUpperCase();


  66.         // convert data if not already a string

  67.         if ( s.data && s.processData && typeof s.data !== "string" ) {

  68.                 s.data = jQuery.param( s.data, s.traditional );

  69.         }


  70.         比较重要的一部分:


  71.         http://localhost/index/ajax?callback=jsonp1274437815229&id=1


  72.         服务器判断是否有这个callback参数,如果有就返回JS函数名+对象


  73.         //jsonp = jsonp1274437815229(请求地址的回调参数)


  74.         //jsonp全局函数

  75.         window[ jsonp ] = window[ jsonp ] || function( tmp ) {

  76.                 data = tmp;

  77.                 success();

  78.                 complete();

  79.                 // Garbage collect

  80.                 window[ jsonp ] = undefined;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值