与现在那些Ajax框架比较,优劣之处?看完再说吧:
1、Ajax.js
复制代码 代码如下:
/*
    AJAX v1.4
    HJF 2009-7-5
*/
function AjaxDO(){

    this.HttpRequest = null;

    this.openMethod = null; //HTTP请求的方法,为Get、Post 或者Head

    this.openURL = null; //是目标URL。基于安全考虑,这个URL 只能是同网域的,否则会提示“没有权限”的错误。

    this.openAsync = null; //是指定在等待服务器返回信息的时间内是否继续执行下面的代码。如果为False,则不会继续执行,直到服务器返回信息。默认为True。

    this.ProcessRequestFunction = function(_HttpRequest) {return;} //处理返回信息的函数入口

    this.ProcessRequestParam = null; //处理访问信息时的附加参数

    this.LoadingImg = null; //正在载入的图片,一般为.gif动画

    //初始化HttpRequest
    this.InitHttpRequest = function(){
        var http;

    //    try {
    //        http = new ActiveXObject("Msxml2.XMLHTTP");
    //    } catch(e) {
    //        try {
    //            http = new ActiveXObject("Microsoft.XMLHTTP");
    //        } catch(e) {
    //            http = false;
    //        }
    //    }

        try    {
            if(window.ActiveXObject){
                for(var i=5; i; i--){
                    try{
                        if(i==2){
                            http = new ActiveXObject("Microsoft.XMLHTTP");
                        }else{
                            http = new ActiveXObject( "Msxml2.XMLHTTP." + i + ".0" );
                        }
                        break;
                    }catch(e){
                        //alert(i);
                        http = false;
                    }
                }
            }else if(window.XMLHttpRequest){
                http = new XMLHttpRequest();
                if(http.overrideMimeType){
                    http.overrideMimeType("text/xml");
                }
            }
        }catch(e){
            http = false;
        }

        if(!http){
            Alert("不能创建XMLHttpRequest对象实例");
            return http;
        }

        this.HttpRequest = http;
        return http;
    }

    //检测 this.HttpRequest
    this.checkHttpRequest = function(){
        if(!this.HttpRequest){
            return this.InitHttpRequest();
        }
        return this.HttpRequest;
    }

    //修改MIME类别
    //http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); //如果要传文件或者Post 内容给服务器
    //http.setRequestHeader("Content-Type","text/xml");
    //http.setRequestHeader("Content-Type","gb2312");
    this.setRequestHeader = function(mime){
        if(!this.checkHttpRequest()){
            return false;
        }
        try{
            this.HttpRequest.setRequestHeader("Content-Type", mime);
            return true;
        }catch(e){
            if(e instanceof Error){
                Alert("修改MIME类别错误");
                return false;
            }
        }
    }

    //设置状态改变的事件触发器
    this.setOnReadyStateChange = function(funHandle, Param){
        if(!this.checkHttpRequest()){
            return false;
        }
        this.ProcessRequestFunction = funHandle;
        this.ProcessRequestParam = Param;
        return true;
    }

    this.setLoadingImg = function(ImgID){
        this.LoadingImg = ImgID;
    }

    //建立HTTP连接
    //open("method","URL"[,asyncFlag[,"userName"[, "password"]]])
    this.Open = function(method, url, async, username, psw){
        if(!this.checkHttpRequest()){
            return false;
        }
        this.openMethod = method;
        this.openURL = url;
        this.openAsync = async;
        if((this.openMethod==null) || ((this.openMethod.toUpperCase()!="GET")&&(this.openMethod.toUpperCase()!="POST")&&(this.openMethod.toUpperCase()!="HEAD"))){
            Alert("请指定HTTP请求的方法,为Get、Post 或者Head");
            return false;
        }
        if((this.openURL==null)||(this.openURL=="")){
            Alert("请指定目标URL");
            return false;
        }
        try{
            this.HttpRequest.open(this.openMethod, this.openURL, this.openAsync, username, psw);
        }catch(e){
            if(e instanceof Error){
                Alert("无法建立HTTP连接");
                return false;
            }
        }

        if(this.openMethod.toUpperCase()=="POST"){
            if(!this.setRequestHeader("application/x-www-form-urlencoded")){
                Alert("修改MIME类别失败");
                return false;
            }
        }

        if(this.openAsync){ //异步模式,程序继续执行
            if(this.ProcessRequestFunction==null){
                Alert("请指定处理返回信息的函数");
                return false;
            }
            var _http_request_ajax = this.HttpRequest;
            var _this_ajax = this;
            this.HttpRequest.onreadystatechange = function(){
                if(_http_request_ajax.readyState==4) {
                    if(_http_request_ajax.status==200) {
                        _this_ajax.ProcessRequestFunction(_http_request_ajax, _this_ajax.ProcessRequestParam, _this_ajax.LoadingImg);
                    }else{
                        Alert("您所请求的页面有异常。");
                        return false;
                    }
                }
            }
        }

        if(this.LoadingImg!=null){
            funShow(this.LoadingImg);
        }

        return true;
    }

    //向服务器发出HTTP请求
    //格式:name=value&anothername=othervalue&so=on
    this.Send = function(idata){
        if(!this.checkHttpRequest()){
            return false;
        }
        var data = null;
        if(this.openMethod.toUpperCase()=="POST"){
            data = funEscapeAll(idata);
        }
        try{
            this.HttpRequest.send(data);
            return true;
        }catch(e){
            if(e instanceof Error){
                Alert("向服务器发出HTTP请求失败");
                return false;
            }
        }
    }

    //处理服务器返回的信息
    this.getResponseText = function(type){
        if(!this.checkHttpRequest()){
            return false;
        }
        if(this.HttpRequest.readyState==4) {
            if(this.HttpRequest.status==200) {
                if((type!=null) && (type.toUpperCase()=="XML")){
                    return this.HttpRequest.responseXML;
                }
                return this.HttpRequest.responseText;
            }else{
                Alert("您所请求的页面有异常。");
                return false;
            }
        }
    }

    //停止当前请求
    this.abort = function(){
        if(!this.checkHttpRequest()){
            return false;
        }

        if(this.LoadingImg!=null){
            funHide(this.LoadingImg);
        }

        if(this.HttpRequest.readyState>0 && this.HttpRequest.readyState<4){
            this.HttpRequest.abort();
        }
    }

}

//=====================================================================================
//公共函数
//=====================================================================================
function $(_obj){
    var o;
    if (typeof(_obj)!="string")
        return _obj;
    else{
        try{
            document.all;
            try{
                o=document.all(_obj);
            }catch(e){
                return null;
            }
        }catch(ee){
            try{
                o=document.getElementById(_obj);
            }catch(e){
                return null;
            }
        }
        return o;
    }
}

function funEscapeAll(str){
    var t = "&";
    var s = str.split(t);
    if(s.length<=0)    return str;
    for(var i=0; i<s.length; i++){
        s[i] = funEscapeOne(s[i]);
    }
    return s.join(t);
}

function funEscapeOne(str){
    var i = str.indexOf("=");
    if(i==-1) return str;
    var t = URLEncode(str.substr(i+1));
    return str.substring(0, i+1)+t;
}

function URLEncode(str){
    return encodeURIComponent(str);
/*    
    return escape(str).replace(/\+/g, "%2B").
                replace(/\"/g,"%22").
                 replace(/\'/g, "%27").
                     replace(/\//g,"%2F");
*/
}

function funEscapeXML(content) {
if (content == undefined)
return "";
if (!content.length || !content.charAt)
content = new String(content);
var result = "";
var length = content.length;
for (var i = 0; i < length; i++) {
var ch = content.charAt(i);
switch (ch) {
case '&':
result += "&";
break;
case '<':
result += "<";
break;
case '>':
result += ">";
break;
case '"':
result += """;
break;
case '\'':
result += "'";
break;
default:
result += ch;
}
}
return result;
}

function funShow(_obj){
    if(typeof(_obj)=="object")
        _obj.style.visibility = "inherit";
    else
        $(_obj).style.visibility = "inherit";
}

function funHide(_obj){
    if(typeof(_obj)=="object")
        _obj.style.visibility = "hidden";
    else
        $(_obj).style.visibility = "hidden";
}

function Alert(str){
    alert(str);
    //window.status = str;
}

/*

使用例子:

function processRequest(http_request, _val, _loading_img){
    if(http_request.responseXML.documentElement){
        //alert(decodeURIComponent(http_request.responseXML.documentElement.xml));
    }else{
        //alert(decodeURIComponent(http_request.responseText));
    }
    alert(_val);
    funHide(_loading_img);
}

1、GET
    var ajax = new AjaxDO();

    ajax.setLoadingImg(_loading_img);
    ajax.setOnReadyStateChange(processRequest, _val);
    ajax.Open("GET", url, true); //异步模式,程序继续执行
    ajax.Send("");

    ajax.Open("GET", url, false); //非异步模式,程序等待
    ajax.Send("");
    var xml_doc = ajax.getResponseText("XML");
    var text_doc = ajax.getResponseText("TEXT");

2、POST
    var ajax = new AjaxDO();

    ajax.setLoadingImg(_loading_img);
    ajax.setOnReadyStateChange(processRequest, _val);
    ajax.Open("POST", url, true); //异步模式,程序继续执行
    ajax.Send(data);

    ajax.Open("POST", url, false); //非异步模式,程序等待
    ajax.Send(data);
    var xml_doc = ajax.getResponseText("XML");
    var text_doc = ajax.getResponseText("TEXT");

    
注,客户端发送带有中文或HTML脚本的信息时,发送的信息必须调用:encodeURIComponent函数,例如:
var data = encodeURIComponent($('message').value);
实际是调用了两次,Ajax类内部又调用一次。

服务端(Java版)需要做下转码:
String message = request.getParameter("message");
message = URLDecoder.decode(message, "UTF-8");

*/

注,客户端发送带有中文或HTML脚本的信息时,发送的信息必须调用:encodeURIComponent函数,例如:
var data = encodeURIComponent($('message').value);
实际是调用了两次,Ajax类内部又调用一次。

服务端(Java版)需要做下转码:

String message = request.getParameter("message");
message = URLDecoder.decode(message, "UTF-8");
2、Demo.html
复制代码 代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>Ajax类</title>
<script src="/UploadFiles/2021-04-02/Ajax.js">
<style type="text/css"><!--
#Layer1 {
    position:absolute;
    left:670px;
    top:11px;
    width:15px;
    height:15px;
    z-index:10000;
    background-color:#FF0000;
    font-size:13;
    border:none;
    visibility:hidden;
}
--></style><style type="text/css" bogus="1">#Layer1 {
    position:absolute;
    left:670px;
    top:11px;
    width:15px;
    height:15px;
    z-index:10000;
    background-color:#FF0000;
    font-size:13;
    border:none;
    visibility:hidden;
}</style>
</head>

<body>
<div id="Layer1"><img src="/UploadFiles/2021-04-02/indicator_flower.gif"><script type="text/javascript" language="javascript"><!--
function processRequest(http_request, _val, _loading_img){
    alert(http_request.responseXML.documentElement.xml);
    //alert(http_request.responseText);
    funHide(_loading_img);
}
// --></script>
<script type="text/javascript" language="javascript"><!--
var url = "http://www.w3schools.com/xml/simple.xml";
var data = "";

var ajax = new AjaxDO();

function btnAjax1(){
    //var ajax = new AjaxDO();
    //ajax.InitHttpRequest();
    ajax.abort();

    ajax.setLoadingImg(document.getElementById("Layer1"));
    ajax.setOnReadyStateChange(processRequest);
    ajax.Open("GET", url, true); //异步模式,程序继续执行
    ajax.Send("");
}

function btnAjax2(){
    //var ajax = new AjaxDO();
    //ajax.InitHttpRequest();
    ajax.abort();

    ajax.Open("GET", url, false); //非异步模式,程序等待
    ajax.Send("");
    alert(ajax.getResponseText("XML").documentElement.xml);
    alert(ajax.getResponseText("TEXT"));
}
// --></script>
<button onclick="btnAjax1()">异步模式</button>
<button onclick="btnAjax2()">非异步模式</button>
</body>
</html>

3、图片

javascript Ajax 类实现代码

广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!

《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线

暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。

艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。

《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。