加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Ajax get和Post的纯js实现代码

发布时间:2020-12-16 00:29:28 所属栏目:百科 来源:网络整理
导读:span style="font-size:18px;"//AJAX类function AjaxClass(){ var XmlHttp = false; try { XmlHttp = new XMLHttpRequest(); //FireFox专有 } catch(e) { try { XmlHttp = new ActiveXObject("MSXML2.XMLHTTP"); } catch(e2) { try { XmlHttp = new ActiveXO
<span style="font-size:18px;">//AJAX类
function AjaxClass()
{
    var XmlHttp = false;
    try
    {
        XmlHttp = new XMLHttpRequest();        //FireFox专有
    }
    catch(e)
    {
        try
        {
            XmlHttp = new ActiveXObject("MSXML2.XMLHTTP");
        }
        catch(e2)
        {
            try
            {
                XmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch(e3)
            {
                alert("你的浏览器不支持XMLHTTP对象,请升级到IE6以上版本!");
                XmlHttp = false;
            }
        }
    }

    var me = this;
    this.Method = "POST";
    this.Url = "";
    this.Async = true;
    this.Arg = "";
    this.CallBack = function(){};
    this.Loading = function(){};
    
    this.Send = function()
    {
        if (this.Url=="")
        {
            return false;
        }
        if (!XmlHttp)
        {
            return IframePost();
        }

        XmlHttp.open (this.Method,this.Url,this.Async);
        if (this.Method=="POST")
        {
            XmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        }
        XmlHttp.onreadystatechange = function()
        {
            if (XmlHttp.readyState==4)
            {
                var Result = false;
                if (XmlHttp.status==200)
                {
                    Result = XmlHttp.responseText;
                }
                XmlHttp = null;
                
                me.CallBack(Result);
            }
             else
             {
				me.Loading();
             }
        }
        if (this.Method=="POST")
        {
            XmlHttp.send(this.Arg);
        }
        else
        {
            XmlHttp.send(null);
        }
    }
    
    //Iframe方式提交
    function IframePost()
    {
        var Num = 0;
        var obj = document.createElement("iframe");
        obj.attachEvent("onload",function(){ me.CallBack(obj.contentWindow.document.body.innerHTML); obj.removeNode() });
        obj.attachEvent("onreadystatechange",function(){ if (Num>=5) {alert(false);obj.removeNode()} });
        obj.src = me.Url;
        obj.style.display = 'none';
        document.body.appendChild(obj);
    }
}

/*----------------------------调用方法------------------------------
    var Ajax = new AjaxClass();			// 创建AJAX对象
    Ajax.Method = "POST"; 				// 设置请求方式为POST
    Ajax.Url = "default.asp"			// URL为default.asp
    Ajax.Async = true;					// 是否异步
    Ajax.Arg = "a=1&b=2";				// POST的参数
    Ajax.Loading = function(){			//等待函数
		document.write("loading...");
    }
    Ajax.CallBack = function(str)       // 回调函数
    {
        document.write(str);
    }
    Ajax.Send();            			// 发送请求
   -----------------------------------------------------------
    var Ajax = new AjaxClass();			// 创建AJAX对象
    Ajax.Method = "GET"; 				// 设置请求方式为POST
    Ajax.Url = "default.asp?a=1&b=2"	// URL为default.asp
    Ajax.Async = true;					// 是否异步
    Ajax.Loading = function(){			//等待函数
		document.write("loading...");
    }
    Ajax.CallBack = function(str)       // 回调函数
    {
        document.write(str);
    }
    Ajax.Send();           				// 发送请求
--------------------------------------------------------------------*/ </span>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读