⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 oajax.lib.js

📁 asp的bbs程序
💻 JS
字号:
/*
  By Hangring
  #2007.01.11#
  ---
  use list:
  > global.lib.js
  > browser.lib.js
  ---
  oAjax
*/

function oAjax () {
    this.req = null;
    this.url = '';
    this.content = '';
    this.type = 'text';
    this.encode = '';
    this.asyn = true;
    this.action = 'get';
    this.error = false;
}

oAjax.prototype.init = function () {
    if (window.XMLHttpRequest) {
        this.req = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {
        // isIE = true;
        try {
            this.req = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
            try {
                this.req = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch(e) {
                this.req = false;
            }
        }
    }
    var self = this;
    if (this.req) {
        this.req.onreadystatechange = function () {self.listener()};
    }
};

oAjax.prototype.listener = function () {
    if (this.req.readyState == 4) {
        if (this.req.status == 200) {
            // right
            try {
                this.callback(Browser.IsIE && this.encode == 'gb2312' ? oAjax.gb2utf8(this.req.responseBody) : (this.type == 'text' ? this.req.responseText : this.req.responseXML));
            }
            catch (e) {
                this.halt('[callback] ' + e.name + ':' + e.message);
            }
        }
        else {
            // error
            this.halt('[callback error] ' + this.req.status);
        }
    }
};

oAjax.prototype.send = function (url) {
    this.init();

    url = this.url = url || this.url || '';
    this.content = !!this.content ? this.content : '';
    this.encode = this.encode ? this.encode.toLowerCase() : '';
    this.asyn = this.asyn == undefined ? true : !!this.asyn;
    this.action = (this.action == undefined || this.action == 'get') ? 'Get' : 'Post';
    this.error = this.error == undefined ? false : !!this.error;

    if (! url && this.error) {
        alert('Ajax请求URL不能为空。');
        return;
    }
    try {
        this.req.open(this.action, url, this.asyn);
    }
    catch (e) {
        this.halt('[open] ' + e.name + ':' + e.message);
        return;
    }
    try {
        this.req.setRequestHeader('Connection', 'close');
        this.req.setRequestHeader('Accept-Encoding', 'gzip, deflate');
        this.req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded' + (this.encode ? ';charset=' + this.encode : ''));
        if(this.req.overrideMimeType && this.encode) {
            this.req.overrideMimeType('text/xml' + (this.encode ? ';charset=' + this.encode : ''));
        }
        this.req.send(this.content);
    }
    catch (e) {
        this.halt('[open] ' + e.name + ':' + e.message + '\n** 检查是否为跨域访问。');
    }
};

oAjax.prototype.callback = function (content) {
    //alert(content);
};

    // abort
oAjax.prototype.abort = function () {
    this.req.abort();
};

oAjax.prototype.halt = function (description) {
    this.error && alert(description);
};

// gb2312 to utf8
oAjax.gb2utf8 = function (data) {
    var glbEncode = [];
    gb2utf8_data = data;
    execScript("gb2utf8_data = MidB(gb2utf8_data, 1)", "VBScript");
    var t = escape(gb2utf8_data).replace(/%u/g,"").replace(/(.{2})(.{2})/g,"%$2%$1").replace(/%([A-Z].)%(.{2})/g,"@$1$2");
    t = t.split("@");
    var i=0, j = t.length, k;
    while(++i < j) {
        k = t[i].substring(0,4);
        if(!glbEncode[k]) {
            gb2utf8_char = eval("0x" + k);
            execScript("gb2utf8_char = Chr(gb2utf8_char)", "VBScript");
            glbEncode[k] = escape(gb2utf8_char).substring(1, 6);
        }
        t[i] = glbEncode[k] + t[i].substring(4);
    }
    gb2utf8_data = gb2utf8_char = null;
    return unescape(t.join("%"));
}

/*
    var ajax = new oAjax();
    // 发送的内容:a=b&b=c&c=d
    ajax.content = '';
    ajax.action = 'get'|'post';
    // 是否显示错误
    ajax.error = true|false;
    // 异步或同步
    ajax.asyn = true|false;
    // 请求的页面的编码为'gb2312'或空
    ajax.encode = ''|'gb2312';
    // 回调函数
    ajax.callback = function (content) {
        // 处理返回内容
    };
    ajax.send(url);
*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -