JQuery的ajax跨域访问方法

本文介绍了一种使用jQuery的getJSON方法解决跨域问题的方法,仅需一行代码即可实现跨域请求。文章详细解释了jQuery跨域原理及如何通过JSONP方式绕过同源策略限制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

JS的跨域问题,我想很多程序员的脑海里面还认为JS是不能跨域的,其实这是一个错误的观点;有很多人在网上找其解决方法,教其用IFRAME去解决的文章很多,真有那么复杂吗?其实很简单的,如果你用JQUERY,一个GETJSON方法就搞定了,而且是一行代码搞定。

下面开始贴出方法。

//跨域(可跨所有域名)
$.getJSON("http://user.hnce.com.cn/getregion.aspx?id=0&jsoncallback=?",function(json){
//要求远程请求页面的数据格式为: ?(json_data) //例如: //?([{"_name":"湖南省","_regionId":134},{"_name":"北京市","_regionId":143}]) alert(json[0]._name);
});
 
$.getJSON("http://user.hnce.com.cn/getregion.aspx?id=0&jsoncallback=?",function(json){
//要求远程请求页面的数据格式为: ?(json_data) //例如: //?([{"_name":"湖南省","_regionId":134},{"_name":"北京市","_regionId":143}]) alert(json[0]._name);
});


 注意,getregion.aspx中,在输出JSON数据时,一定要用Request.QueryString["jsoncallback"],将获取的内容放到返回JSON数据的前面,假设实际获取的值为42342348,那么返回的值就是 42342348([{"_name":"湖南省","_regionId":134},{"_name":"北京市","_regionId":143}])

因为getJSON跨域的原理是把?随机变一个方法名,然后返回执行的,实现跨域响应的目的。

下面一个是跨域执行的真实例子:

?
<script src="http://common.cnblogs.com/script/jquery.js"type="text/javascript"></script>
<script type="text/javascript">
//跨域(可跨所有域名)
$.getJSON("http://e.hnce.com.cn/tools/ajax.aspx?jsoncallback=?", { id: 0, action: 'jobcategoryjson'}, function(json) { alert(json[0].pid); alert(json[0].items[0]._name); });  
 </script>

jQuery跨域原理:

浏览器会进行同源检查,这导致了跨域问题,然而这个跨域检查还有一个例外那就是HTML的<Script>标记;我们经常使用<Script>的src属性,脚本静态资源放在独立域名下或者来自其它站点的时候这里是一个url;这个url响应的结果可以有很多种,比如JSON,返回的Json值成为<Script>标签的src属性值.这种属性值变化并不会引起页面的影响.按照惯例,浏览器在URL的查询字符串中提供一个参数,这个参数将作为结果的前缀一起返回到浏览器;

看下面的例子:

<script type="text/javascript"src="http://domain2.com/getjson?jsonp=parseResponse"> </script> 
响应值:parseResponse({"Name":"Cheeso","Rank": 7}) 
<script type="text/javascript"src="http://domain2.com/getjson?jsonp=parseResponse"> </script> 
响应值:parseResponse({"Name":"Cheeso","Rank": 7}) 

这种方式被称作JsonP;(如果链接已经失效请点击这里:JSONP);即:JSON with padding 上面提到的前缀就是所谓的“padding”。那么jQuery里面是怎么实现的呢?

貌似并没有<Script>标记的出现!?OKay,翻看源码来看:

页面调用的是getJSON:

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

 继续跟进

get:function( url, data, callback, type ) {
 // shift arguments if data argument was omited
 if( jQuery.isFunction( data ) ) {
  type = type || callback;
  callback = data;
  data = null;
 }
 
 returnjQuery.ajax({
  type:"GET",
  url: url,
  data: data,
  success: callback,
  dataType: type
 });

跟进jQuery.ajax,下面是ajax方法的代码片段:

// Build temporary JSONP function
 if( s.dataType === "json"&& (s.data && jsre.test(s.data) || jsre.test(s.url)) ) {
  jsonp = s.jsonpCallback || ("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 ] = window[ jsonp ] || function( tmp ) {
   data = tmp;
   success();
   complete();
   // Garbage collect
   window[ jsonp ] = undefined;
 
   try{
    deletewindow[ jsonp ];
   }catch(e) {}
 
   if( head ) {
    head.removeChild( script );
   }
  };
 }
 
 if( s.dataType === "script"&& s.cache === null) {
  s.cache = false;
 }
 
 if( s.cache === false&& type === "GET") {
  varts = now();
 
  // try replacing _= if it is there
  varret = s.url.replace(rts, "$1_="+ ts + "$2");
 
  // if nothing was replaced, add timestamp to the end
  s.url = ret + ((ret === s.url) ? (rquery.test(s.url) ? "&": "?") + "_="+ ts : "");
 }
 
 // If data is available, append data to url for get requests
 if( s.data && type === "GET") {
  s.url += (rquery.test(s.url) ? "&": "?") + s.data;
 }
 
 // Watch for a new set of requests
 if( s.global && ! jQuery.active++ ) {
  jQuery.event.trigger("ajaxStart");
 }
 
 // Matches an absolute URL, and saves the domain
 varparts = rurl.exec( s.url ),
  remote = parts && (parts[1] && parts[1] !== location.protocol || parts[2] !== location.host);
 
// If we're requesting a remote document
 // and trying to load JSON or Script with a GET
 if( s.dataType === "script"&& type === "GET"&& remote ) {
  varhead = document.getElementsByTagName("head")[0] || document.documentElement;
  varscript = document.createElement("script");
  script.src = s.url;
  if( s.scriptCharset ) {
   script.charset = s.scriptCharset;
  }
 
  // Handle Script loading
  if( !jsonp ) {
   vardone = false;
 
   // Attach handlers for all browsers
   script.onload = script.onreadystatechange = function() {
    if( !done && (!this.readyState ||
      this.readyState === "loaded"|| this.readyState === "complete") ) {
     done = true;
     success();
     complete();
 
     // Handle memory leak in IE
     script.onload = script.onreadystatechange = null;
     if( head && script.parentNode ) {
      head.removeChild( script );
     }
    }
   };
  }
 
  // Use insertBefore instead of appendChild to circumvent an IE6 bug.
  // This arises when a base node is used (#2709 and #4378).
  head.insertBefore( script, head.firstChild );
 
  // We handle everything using the script element injection
  returnundefined;
 }

上面的代码第1行到第10行:判断是JSON类型调用,为本次调用创建临时的JsonP方法,并且添加了一个随机数字,这个数字源于用日期值;

 关注第14行,这一行相当关键,注定了我们的结果最终是<Script> ;然后是构造Script片段,第95行在Head中添加该片段,修成正果;

 不仅仅是jQuery,很多js框架都是用了同样的跨域方案,这就是getJSON跨域的原理。


转自:http://www.jb51.net/article/77470.htm

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值