首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

跨域访问的最终解决方案

2012-10-05 
跨域访问的终极解决方案方案一:使用jsonp ,script src/jquery-1.4.2.min.js typetext/javascript

跨域访问的终极解决方案
方案一:使用jsonp ,
<script src='/jquery-1.4.2.min.js' type='text/javascript' > </script>

<script>
      jQuery.getJSON("http://61.143.165.67:8088/GameManager/addCount?type=1&jsoncallback=?",
    function(data) {     

    }
);

</script>

方案二:使用p3p0 设置头文件
方案三:使用服务器端语言,例如php
<?
//发送POST请求
function sendPost($url, $data, $timeout = 10) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
$html = curl_exec($ch);
curl_close($ch);
return $html;
}

//发送GET请求
function sendGet($url, $timeout = 10) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
$html = curl_exec($ch);
curl_close($ch);
return $html;
}
方案四:
falsh中的方案
/**
* @author Kinglong
* @version 0.5
*/ 
 
package com.klstudio.upload { 
     
    import flash.display.*; 
    import flash.events.*; 
    import flash.net.*; 
    import flash.external.ExternalInterface; 
    import flash.system.Security;    
 
    public class FlashProxy extends Sprite{  
         
        private var _params:Object; 
        private var _loader:URLLoader; 
        private var _method:String; 
        private var _id:String; 
        private var _parse:Boolean; 
        private var _inited:Boolean; 
        private var _data:String; 
        private var _xml:XML; 
         
        public function FlashProxy(){ 
            Security.allowDomain('*'); 
            _inited = false; 
            _data = ""; 
            _parse = true; 
            _method = URLRequestMethod.POST; 
            _loader = new URLLoader(); 
            configureListeners(_loader); 
        } 
         
        public function load(page:String,params:Object=null):void{ 
            if(!_inited){ 
                showError("FlashProxy还没有初始化!"); 
            } 
            if(page == null || page == undefined){ 
                showError("page参数为必须的!"); 
                return; 
            } 
            var vars:URLVariables = new URLVariables(); 
            vars.tmp = Math.random(); 
            if(params != null){              
                for(var key in params){ 
                    if(key == "tmp"){ 
                        continue; 
                    } 
                    vars[key] = params[key]; 
                } 
            }            
            _loader.dataFormat = URLLoaderDataFormat.TEXT; 
            var request:URLRequest = new URLRequest(page); 
            request.method = _method; 
            request.data = vars; 
            _loader.load(request); 
        } 
         
        public function setParse(value:Boolean):void{ 
            _parse = value; 
        } 
         
        public function setMethod(value:String):void{ 
            if(value == null || value == undefined){ 
                return; 
            } 
            value = StringUtil.trim(value); 
            if(value.toLocaleUpperCase() == URLRequestMethod.POST || value.toLocaleUpperCase() == URLRequestMethod.GET){ 
                _method = value.toLocaleUpperCase(); 
            }            
        } 
         
        public function getData():String{ 
            return _data; 
        } 
         
        private function configureListeners(dispatcher:IEventDispatcher):void { 
            dispatcher.addEventListener(Event.COMPLETE, completeHandler); 
            dispatcher.addEventListener(Event.OPEN, openHandler); 
            dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler); 
            dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler); 
            dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler); 
            dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler); 
        } 
         
        private function completeHandler(event:Event):void { 
            _data = _loader.data; 
            if(_parse){ 
                try{ 
                    _xml = new XML(_data);                   
                }catch(error:Error){ 
                    _parse = false; 
                    _xml = null; 
                    Tracer.debug("xml解析错误!"); 
                } 
            } 
            ExternalInterface.call(_id+".onComplete"); 
        } 
 
        private function openHandler(event:Event):void { 
            ExternalInterface.call(_id+".onOpen"); 
        } 
 
        private function progressHandler(event:ProgressEvent):void {             
            ExternalInterface.call(_id+".onProgress",event.bytesLoaded,event.bytesTotal); 
        } 
 
        private function securityErrorHandler(event:SecurityErrorEvent):void { 
            showError("读取失败!\n  不能跨域访问,可能是你在web服务端根目录下没有放置crossdomain.xml文件!"); 
        } 
 
        private function httpStatusHandler(event:HTTPStatusEvent):void { 
            showError("读取失败!\n  HTTP错误代码为:"+event.status); 
        } 
 
        private function ioErrorHandler(event:IOErrorEvent):void { 
            showError("读取失败!\n  可能是因为下列原因:\n    1.网络忙,请稍候重试!\n    2.访问的web服务端没有开启!"); 
        } 
         
        public function alert(msg:String):void{ 
            navigateToURL(new URLRequest(msg)); 
        } 
         
        public function showError(msg:String):void{          
            ExternalInterface.call(_id+".onError",msg); 
        } 
    } 
     

热点排行