简易AJAX框架

来源:互联网 发布:ubuntu下安装emacs 编辑:程序博客网 时间:2024/05/17 09:21
var Ajax = function(){};

//取得组件
Ajax.prototype.GetHttpRequest = function()
{
    if (window.XMLHttpRequest)
    {
        return new XMLHttpRequest() ;
    }
    else if (window.ActiveXObject)
    {
        return new ActiveXObject("Microsoft.XMLHTTP") ;
    }
}
//提交数据
Ajax.prototype.PostURL = function(URL, PostData , FunctionName)
{
    var TempAjax = this;
    var IsFunction = (typeof(FunctionName) == 'function');
    var TempXmlHttp = this.GetHttpRequest();
    TempXmlHttp.open("POST", URL, IsFunction) ;
    if (IsFunction)
    {
        TempXmlHttp.onreadystatechange = function()
        {
            if ( TempXmlHttp.readyState == 4)
            {
                var Result = TempXmlHttp.responseText;
                TempAjax.DOMDocument = TempXmlHttp.responseXML ;
                FunctionName(Result) ;
            }
        }
    }
    if (this.debug == 1)
    {
        alert("Error");
    }
    TempXmlHttp.setRequestHeader("CONTENT-TYPE","application/x-www-form-urlencoded");
    TempXmlHttp.send(PostData) ;
    if (!IsFunction)
    {
        this.DOMDocument = TempXmlHttp.responseXML ;
    }
}
//请求数据
Ajax.prototype.RequestURL = function(URL,FunctionName)
{
    var TempAjax = this;
    var IsFunction = (typeof(FunctionName) == 'function');
    var TempXmlHttp = this.GetHttpRequest();
    TempXmlHttp.open("GET",URL,IsFunction);
    if (IsFunction)
    {
        TempXmlHttp.onreadystatechange = function()
        {
            if ( TempXmlHttp.readyState == 4 )
            {
                var Result = TempXmlHttp.responseText;
                TempAjax.DOMDocument = TempXmlHttp.responseXML;
                FunctionName(Result);
            }
        }
    }
    TempXmlHttp.send(null);
    if (!IsFunction)
    {
        this.DOMDocument = TempXmlHttp.responseXML;
    }
}
//读取XML节
Ajax.prototype.SelectNodes = function(Xpath)
{
    if (document.all)
    {
        return this.DOMDocument.selectNodes(Xpath) ;
    }
    else
    {
        var NodeArray = new Array();
        var PathResult = this.DOMDocument.evaluate(Xpath,this.DOMDocument,this.DOMDocument.createNSResolver(this.DOMDocument.documentElement),XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
        if (PathResult)
        {
            var TempNode = PathResult.iterateNext() ;
            while(TempNode)
            {
                NodeArray[NodeArray.length] = TempNode ;
                TempNode = PathResult.iterateNext();
            }
        }
        return NodeArray;
    }
}
//读取单个XML字内容
Ajax.prototype.SelectSingleNode = function(Xpath)
{
    if (document.all)
    {
        return this.DOMDocument.selectSingleNode(Xpath);
    }
    else
    {
        var PathResult = this.DOMDocument.evaluate(Xpath,this.DOMDocument,this.DOMDocument.createNSResolver(this.DOMDocument.documentElement),9,null);
        if (PathResult && PathResult.singleNodeValue )
        {
            return PathResult.singleNodeValue;
        }
        else
        {
            return null ;
        }
    }
}
原创粉丝点击