软件人生

Jack(子游)

我的目标:做世界一流的软件,成为优秀的项目管理者 主要专注行业: Cms(content manage system) OA CRM 在线营销系统 在线调查
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

ajax xmlhttprequest

Posted on 2007-12-03 23:07  子游  阅读(205)  评论(0)    收藏  举报
    function yzXmlRequest(url,params,method,async,username,password)
    {
        this.url=url;
        this.params=params;
        this.method=method||'POST';
        this.async=(async!=null)?async:true;
        this.username=username;
        this.password=password;
    };
    yzXmlRequest.prototype.url=null;
    yzXmlRequest.prototype.params=null;
    yzXmlRequest.prototype.method=null;
    yzXmlRequest.prototype.async=null;
    yzXmlRequest.prototype.username=null;
    yzXmlRequest.prototype.password=null;
    yzXmlRequest.prototype.request=null;
    
    yzXmlRequest.prototype.isReady=function()
    {
         return this.request.readyState==4;
    }
    yzXmlRequest.prototype.getDocumentElement=function()
    {
        var doc=this.getXml();
        if(doc!=null)
        {
        return doc.documentElement;
        }
        return null;
    };
    yzXmlRequest.prototype.getXml=function()
    {
        var xml=this.request.responseXML;
        if(xml==null||xml.documentElement==null)
        {
        xml=mxUtils.parseXml(this.request.responseText);
        }
        return xml;
    };
    yzXmlRequest.prototype.getText=function()
    {
        return this.request.responseText;
    };
    yzXmlRequest.prototype.getStatus=function()
    {
        return this.request.status;
    };
    yzXmlRequest.prototype.create=function()
    {
        if(window.XMLHttpRequest)
        {
            return function()
            {
                return new XMLHttpRequest();
            };
        }
        else if(typeof(ActiveXObject)!="undefined")
        {
            return function()
            {
                return new ActiveXObject("Microsoft.XMLHTTP");
            };
        }
    }();
    
    yzXmlRequest.prototype.send=function(onload,onerror)
    {
        this.request=this.create();
        if(this.request!=null)
        {
            var self=this;
            this.request.onreadystatechange=function()
            {
                if(self.isReady())
                {
                    if(onload!=null)
                    {
                        onload(self);
                    }
                }
            }
            this.request.open(this.method,this.url,this.async,this.username,this.password);
            if(this.params!=null)
            {
                this.request.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
            }
            this.request.send(this.params);
        }
    };
快乐软件人生