Ajax跨域请求
各浏览器HTTP Get请求URL最大长度并不相同,几类常用浏览器最大长度及超过最大长度后提交情况如下: IE6.0 :url最大长度2083个字符,超过最大长度后无法提交。 微软的权威解释,IE的url最大长度是2083个字节,可以用于GET传递数据的长度是2048个字节 方法一、document.domain+iframe的设置 document.domain=‘a.com’; varifr=document.createElement(‘iframe’); ifr.src=‘http://script.a.com/b.html‘; ifr.style.display=‘none’; document.body.appendChild(ifr); ifr.onload=function(){ vardoc=ifr.contentDocument||ifr.contentWindow.document; //在这里操纵b.html alert(doc.getElementsByTagName(“h1″)[0].childNodes[0].nodeValue); }; script.a.com上的b.html document.domain=‘a.com’; 这种方式适用于{www.kuqin.com,kuqin.com,script.kuqin.com,css.kuqin.com}中的任何页面相互通信。 源文档<http://www.cnblogs.com/zxktxj/archive/2012/02/23/2365019.html> 方法二、Jquery的Ajax的JSonP getDataByJsonp:function(u,p,f){try{$.ajax({type:”get”,url:u,data:p,dataType:”jsonp”,success:function(msg){f(msg);}});}catch(e){}}//var_url=”http://my2.xcar.com.cn/interface/ajax_friend_jk_cms.php”;var_param=”action=”+act+”&fuid=”+uid+”?callback=?”; XcarApp.Tools.getDataByJsonp(_url,_param,function(msg){Console.log(msg);}) functionJsonp(o){ varrand=“Jsonp”+parseInt(Math.random()*10000); var_url=o.url+”&callback=”+rand; varscript=document.createElement(“script”); script.id=rand; script.src=_url; window[rand]=function(e){ o.callback(e); window[rand]=null; script.parentNode.removeChild(script); } document.getElmentsByTagName(‘head’)[0].appendChild(script); }
$data=array(‘msgData’=>’Success’,’errno’=>0); Echo $_GET["callback"].”(“.json_encode($data).”)”;
1))客户端动态创建<script>标签,且设置请求地址为务器端地址,大概如下:
<scriptid=”callbackJS”src=”http://test.xcar.comm.cn/1.php”><script>
2) 在服务器接收端,会动态地输出JS 代码,如下:
<?php echo“vara={u:2323,name:’xxxx’}”; ?>
3)客户端监测<script>标签的加载状态,会将服务端输出的结果显示在script标签之间,我们再利用加载完成后回调函数进行下一步处理
<scriptid=”callbackJS”src=”http://test.xcar.comm.cn/1.php”> vara={u:2323,name:’xxxx’} <script>
functionloadScript(url,callback){ varscript=document.createElement('script'); script.type="text/javascript"; if(script.readyState){//IE script.onreadystatechange=function(){ if(script.readyState=='loaded'||script.readyState=='complete'){ script.onreadystatechange=null; callback(); } } }else{//!IE script.onload=function(){ callback(); } } script.src=url; document.getElementsByTagName('head')[0].appendChild(script); } [/code]
functionstartRequest(){ varifr=document.createElement('iframe'); ifr.style.display='none'; ifr.src=' http://www.cnblogs.com/lab/cscript/cs2.html#paramdo '; document.body.appendChild(ifr); } functioncheckHash(){ try{ vardata=location.hash?location.hash.substring(1):''; if(console.log){ console.log('Nowthedatais'+data); } }catch(e){}; } setInterval(checkHash,2000); cnblogs.com 域名下的 cs2.html: // 模拟一个简单的参数处理操作 switch(location.hash){ case'#paramdo': callBack(); break; case'#paramset': //dosomething…… break; } functioncallBack(){ try{ parent.location.hash='somedata'; }catch(e){ //ie 、 chrome 的安全机制无法修改 parent.location.hash , // 所以要利用一个中间的 cnblogs 域下的代理 iframe varifrproxy=document.createElement('iframe'); ifrproxy.style.display='none'; ifrproxy.src=' http://a.com/test/cscript/cs3.html#somedata ';// 注意该文件在 "a.com" 域下 document.body.appendChild(ifrproxy); } } a.com下的域名cs3.html 实现起来基本步骤如下: <scripttype="text/javascript"> varstate=0,iframe=document.createElement('iframe'),loadfn=function(){ if(state===1){ vardata=iframe.contentWindow.name;// 读取数据 alert(data);// 弹出 'Iwasthere!' }elseif(state===0){ state=1; iframe.contentWindow.location=" http://a.com/proxy.html ";// 设置的代理文件 } }; iframe.src=' http://b.com/data.html '; if(iframe.attachEvent){ iframe.attachEvent('onload',loadfn); }else{ iframe.onload=loadfn; } document.body.appendChild(iframe); </script> 获取数据以后销毁这个iframe,释放内存;这也保证了安全(不被其他域frame js访问)。 <scripttype="text/javascript"> iframe.contentWindow.document.write(''); iframe.contentWindow.close(); document.body.removeChild(iframe); </script> 总结起来即:iframe的src属性由外域转向本地域,跨域数据即由iframe的window.name从外域传递到本地域。这个就巧妙地绕过了浏览器的跨域访问限制,但同时它又是安全操作。
方法六 、代理方法proxy.html 【原理】:假设两个域名info.xcar.com.cn newxcar.com.cn 。在要进行跨域的两个域名的根目录下都放一个proxy.html的文件。即info.xcar.com.cn/proxy.html; newxcar.com.cn/proxy.html 。在info.xcar.com.cn下的一个页面中创建一个iframe并且指向newxcar.com.cn下的一个页面。 。在iframe引用的面面中(newxcar.com.cn),动态创建一个html script标签,让这个标签的SRC指向info.xcar.com.cn域名的proxy.html文件(还可以传递参数) 。这位就可以在proxy.html中来操作info.xcar.com.cn下面iframe的宽高了。 在爱卡评论系统中就涉及到了这个问题,用的就是这个方法。 方法七 、 原始script标签 利用JSONP协议,等同方法2 jQuery的实现 JSONP是一个非官方的协议,它允许在服务器端集成Script tags返回至客户端,通过javascript callback的形式实现跨域访问(这仅仅是JSONP简单的实现形式)。JSON系统开发方法是一种典型的面向数据结构的分析和设计方法,以活动为中心,一连串的活动的顺序组合成一个完整的工作进程。 之所以会有跨域这个问题的产生根本原因是浏览器的同源策略限制,理解同源策略的限制同源策略是指阻止代码获得或 者更改从另一个域名下获得的文件或者信息。也就是说我们的请求地址必须和当前网站的地指相同。同源策略通过隔离来实现对资源的保护。这个策略的历史非常悠 久从Netscape Navigator 2.0时代就开始了。 解决这个限制的一个相对简单的办法就是在服务器端发送请求,服务器充当一个到达第三方资源的代理中继。虽然是用广泛但是这个方法却不够灵活。 实现原理: 1))客户端动态创建<script>标签,且设置请求地址为务器端地址,大概如下: <script id="callbackJS" src="http://test.xcar.comm.cn/1.php&callback=Calbackxxxxx3234234234"><script>
2) 在服务器接收端,会动态地输出JS 代码,如下: <?php $callback=$_GET["callback"]; echo$callback.”({errno:4001,msg:’xxxx’})”; ?>
3)客户端监测<script>标签的加载状态,会将服务端输出的结果显示在script标签之间,我们再利用加载完成后回调函数进行下一步处理
functionjsonpCallback(data){ alert(data.uname); } <scriptsrc=”http://www.comment.com/test.php?jsonp=jsonpCallback”></script>
其实就是在函数里定义了CallBack函数,返回成功后,直接能在window对象里找到它。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |