新技术论坛
搜索
查看: 917|回复: 0
打印 上一主题 下一主题

[JS/AJAX] Ajax跨域代理访问网络资源的实现代码

[复制链接]
  • TA的每日心情
    开心
    2016-12-9 18:18
  • 签到天数: 85 天

    连续签到: 1 天

    [LV.6]常住居民II

    扫一扫,手机访问本帖
    楼主
    跳转到指定楼层
    发表于 2016-3-20 02:06:47 来自手机 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
        解决Ajax跨域访问报“没有权限”的错误。需要的朋友可以参考下。


    smart.asp
    复制代码 代码如下:
    <script language="jscript" runat="server">
    /*
    在VBS里面的调用方法
    dim myhttp
    set myhttp = SmartHttp(url,method,data); //三个参数均可选
    属性:
    url:String,请求的URL地址
    method:String,请求的方法
    data:String,请求的数据
    charset:String,请求的URL返回数据的编码
    status:Int,请求返回的状态码
    readyState:Int,同HTTP请求的当前通讯状态,1、2、3、4
    dataset:Object,请求的数据,如果增加了,会将这部分数据附加到data属性
    dataset属性:
    charset:String,发送数据的编码
    dataset方法:
    append(key,value,noencode):添加数据
    remove(key):移除某个数据项
    isexists(key):判断某个数据项是不是存在
    clear:清除所有数据项
    方法:
    header(headstr):设置请求头,项和值之间用:分开
    timeout(t1,t2,t3,t4):设置超时时间
    send():发送请求
    getbinary:获取服务器返回的二进制数据
    gettext(charset):获取指定编码的文本
    getjson(charset):获取指定编码的json数据
    getheader(key):获取服务器返回的响应头
    getxml(charset):获取指定编码的xml数据
    */
    function SmartHttp(url,method,data){
    return new _SmartHttp(url,method,data);
    }

    function _SmartHttp(url,method,data){
    if(typeof method=="undefined") method="GET";
    if(typeof data=="undefined") data="";
    method = method.toUpperCase();
    method = method!="OST" ? "GET" : "OST";
    this.timeout=[10000,10000,10000,10000];
    this.method = method;
    this.url=url;
    this.data=data;
    this.charset="gb2312";
    this.http=null;
    this.headers=[];
    this.status=0;
    this.readyState=0;
    this.content=null;
    this.msg="";
    this.dataset={
    charset:"gb2312",
    data:[],
    append:function(key,value,noencode){
    var fn=null;
    if(this.charset.toLowerCase()=="utf-8"){fn = encodeURIComponent;}else{fn = escape;}
    if(noencode==true){fn=function(_str){return _str;}}
    this.data.push({"key":fn(key),"value":fn(value)});
    },
    remove:function(key){
    if(this.data.length<=0) return false;
    var _data=[];
    for(var i=0;i<this.data.length;i++){
    if(this.data.key!=key){
    _data.push(this.data);
    }
    }
    this.data = _data;
    },
    isexists:function(key){
    if(this.data.length<=0) return false;
    for(var i=0;i<this.data.length;i++){
    if(this.data.key==key){
    return true;
    }
    }
    return false;
    },
    clear:function(){
    this.dataset.data=[];
    }
    };
    }

    _SmartHttp.prototype.init=function(){
    var datasetstr="";
    if(this.dataset.data.length>0){
    for(var i=0;i<this.dataset.data.length;i++){
    datasetstr += this.dataset.data.key + "=" + this.dataset.data.value + "&";
    }
    }
    if(datasetstr!="") datasetstr = datasetstr.substr(0,datasetstr.length-1);
    if(this.data==""){this.data = datasetstr;}else{if(datasetstr!="")this.data+= "&" + datasetstr;}
    if(this.data=="")this.data=null;
    //this.url += ((this.url.indexOf("?")<0) ? "?" : "&") + "jornd=" + this.getrnd();
    if(this.method=="GET" && this.data!=null) this.url += "&" + this.data;
    if(this.method=="OST") this.headers.push("Content-Type:application/x-www-form-urlencoded");
    if(!this.charset || this.charset=="") this.charset = "gb2312";
    };

    _SmartHttp.prototype.header=function(headstr){
    if(headstr.indexOf(":")>=0) this.headers.push(headstr);
    return this;
    };

    _SmartHttp.prototype.timeout=function(){
    if(arguments.length>4){return this;}
    for(var i =0;i<arguments.length;i++){
    if(!isNaN(arguments)){
    this.timeout = parseInt(arguments);
    }
    }
    return this;
    };

    _SmartHttp.prototype.send=function(){
    this.init();
    var _http = this.getobj();
    if(_http==null){return this;}
    try{
    _http.setTimeouts(this.timeout[0], this.timeout[1], this.timeout[2], this.timeout[3]);
    }catch(ex){}
    _http.open(this.method,this.url,false);
    if(this.headers.length>0){
    for(var i=0;i<this.headers.length;i++){
    var Sindex = this.headers.indexOf(":");
    var key = this.headers.substr(0,Sindex);
    var value = this.headers.substr(Sindex+1);
    _http.setRequestHeader(key,value);
    }
    }
    _http.send(this.data);
    this.readyState = _http.readyState;
    if(_http.readyState==4){
    this.status = parseInt(_http.status);
    this.http = _http;
    this.content = _http.responseBody;
    }
    return this;
    }

    _SmartHttp.prototype.getbinary=function(){
    return this.content;
    };

    _SmartHttp.prototype.gettext=function(charset){
    try{
    return this.b2s(this.content,charset ? charset : this.charset);
    }catch(ex){
    this.msg = ex.description;
    return "";
    }
    };

    _SmartHttp.prototype.getjson=function(charset){
    try{
    var _json=null;
    eval("_json=(" + this.gettext(charset) + ");");
    return _json;
    }catch(ex){
    this.msg = ex.description;
    return null;
    }
    };

    _SmartHttp.prototype.getheader=function(key){
    if(key){
    if(key.toUpperCase()=="SET-COOKIE"){
    key = key.replace("-","\-");
    var headers = this.http.getAllResponseHeaders();
    var regexp = new RegExp("\n" + key + "\.+?)\r","ig");
    var resstr = "";
    while((res = regexp.exec(headers))!=null){
    var val = res[1].trim();
    resstr = resstr + val.substr(0,val.indexOf(";")) + "; "
    }
    if(resstr!=""){
    resstr = resstr.substr(0,resstr.lastIndexOf(";"));
    }
    return resstr;
    }else{
    return this.http.getResponseHeader(key);
    }
    }else{return this.http.getAllResponseHeaders();}
    };

    _SmartHttp.prototype.getxml=function(charset){
    try{
    var _dom = new ActiveXObject("MSXML2.DOMDocument");
    _dom.loadXML(this.gettext(charset));
    return _dom;
    }catch(ex){
    this.msg = ex.description;
    return null;
    }
    };
    _SmartHttp.prototype.getobj = function (){
    var b=null;
    var httplist = ["MSXML2.serverXMLHttp.3.0","MSXML2.serverXMLHttp","MSXML2.XMLHttp.3.0","MSXML2.XMLHttp","Microsoft.XMLHttp"];
    for(var i = 0;i<=httplist.length -1;i++){
    try{
    b= new ActiveXObject(httplist);
    (function(o){
    _SmartHttp.prototype.getobj = function(){return new ActiveXObject(o)};
    })(httplist);
    return b;
    }catch(ex){
    eval("this.msg = ex.description;");
    }
    }
    return b;
    };

    _SmartHttp.prototype.getrnd = function (){return Math.random().toString().substr(2);};

    _SmartHttp.prototype.b2s = function(bytSource, Cset){ //ef bb bf,c0 fd
    var Objstream,c1,c2,c3;
    var byts;
    Objstream =Server.CreateObject("ADODB.Stream");
    Objstream.Type = 1;
    Objstream.Mode = 3;
    Objstream.Open();
    Objstream.Write(bytSource);
    Objstream.Position = 0;
    Objstream.Type = 2;
    Objstream.CharSet = Cset;
    byts = Objstream.ReadText();
    Objstream.Close();
    Objstream = null;
    return byts;
    };
    _SmartHttp.prototype.urlencode=function(str){ return encodeURIComponent(str);};
    _SmartHttp.prototype.urldecode=function(str){ return decodeURIComponent(str);};
    String.prototype.trim = function(){return this.replace(/(^(\s+)|(\s+)$)/igm,"");};
    </script>

    使用代码:
    复制代码 代码如下:
    <!--#include file="smart.asp"-->
    <%
    Response.charset="utf-8"
    dim url,method,data,charset
    url =Request.Form("targeturl")
    method =Request.Form("method")
    data =Request.Form("data")
    charset = Request.Form("charset")
    if charset = "" then charset = "GB2312"
    response.Write SmartHttp(url,method,data).send().gettext(charset)
    set myhttp = nothing
    %>

    高级模式
    B Color Image Link Quote Code Smilies

    本版积分规则

    手机版|Archiver|开发者俱乐部 ( ICP/ISP证:辽B-2-4-20110106号 IDC证:辽B-1-2-20070003号 )

    GMT+8, 2025-1-4 02:43 , Processed in 0.116408 second(s), 19 queries .

    X+ Open Developer Network (xodn.com)

    © 2009-2017 沈阳讯网网络科技有限公司

    快速回复 返回顶部 返回列表