ajax 小框架

var  ajax  =  function(){};

var  IsUndefined  =  function(v)  {
    return  typeof v  ==  'undefined'  ?  true  :  false;
}

ajax.CreateXMLHttp  =  function()  {
    if(window.XMLHttpRequest)
          return new XMLHttpRequest();
          else  if(window.ActiveXObject){
                try { return  new  ActiveXObject("Microsoft.XMLHTTP"); }
                catch(e){
                        try{return  new  ActiveXObject("Msxml2.XMLHTTP");}
                        catch(e){
                                alert("XMLHttp  object  could  not  be  created: " + e);
                                throw  new  Error("XMLHttp  object  could  not  be  created.");
                        }
                }
            }
}


ajax.Request  =  function(url,func,isxml,ispost,parameters)
{
    var  xhr=this.CreateXMLHttp();
    if(IsUndefined(ispost))  ispost=null;
    if(ispost)
    {
            xhr.open("POST",url,true);
            xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
    }
    else
            xhr.open("GET",url,true);
    if(func){
        xhr.onreadystatechange=function()
        {
            if(xhr.readyState == 4)
            {
                    if(xhr.status == 200)
                        func(isxml && xhr.responseXML ? xhr.responseXML : xhr.responseText);
                    else
                        alert("xhr.status!=200");
            }           
        }
    }
    xhr.setRequestHeader("X-Requested-With",  "XMLHttpRequest");
        if(!IsUndefined(parameters))
                xhr.send(parameters);
        else
            xhr.send(null);
}
/*用Get方式提交数据*/
ajax.Get  =  function(url,func)
{
        this.Request(url,func,false,false);
}
/*用Post方式提交数据*/
ajax.Post  =  function(url,func,parameters)
{
        this.Request(url,func,false,true,parameters);
}

 

posted @ 2008-11-23 22:12  hesion001  阅读(190)  评论(0编辑  收藏  举报