自己写的封装好的简单的AJAX--javascript

  
//Ajax Function
 
var reqObj; //Creat Null Instence
 
//Run Ajax (string urladdress,bool IsAsy,string method,string parameters)
function DoRequest(url,isAsy,method,parStr) 
{
 
    reqObj = false;
 
    if (window.XMLHttpRequest) //compatible Mozilla, Safari,...
    {
       
        reqObj = new XMLHttpRequest();              //Creat XMLHttpRequest Instance
       
        if (reqObj.overrideMimeType)                //if Mime Type is false ,then set MimeType 'text/xml'
        {
            reqObj.overrideMimeType('text/xml');
        }
   
    }
   
    else if (window.ActiveXObject) //compatible IE
    {
   
        try
        {
            reqObj = new ActiveXObject("Msxml2.XMLHTTP"); //Creat XMLHttpRequest Instance
       }
        catch (e)
        {
            try
            {
                reqObj = new ActiveXObject("Microsoft.XMLHTTP"); //Creat XMLHttpRequest Instance
            }
            catch (e)
            {}
        }
   
    }
 
    //if reqObj is false,then alert warnning
    if (!reqObj)
    {
       
        alert('Giving up :( Cannot create an XMLHTTP instance');
        return false;
   
    }
   
   
    reqObj.onreadystatechange = GetRequest; //set onreadystatechange Function
   
    reqObj.open(method, url, isAsy);        //set open Function
   
    reqObj.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); //set RequestHeader
   
    reqObj.send(parStr);   //do send and send parameters
 
}
 
 
//get Service Response information Function
function GetRequest()
{
 
    //judge readystate information
    if (reqObj.readyState == 4)
    {
        //judge status information
        if (reqObj.status == 200)
        {
            Ajax(reqObj);   //do custom Function at Ajax() and trans parameter reqObj
        }
        else
        {
            alert('There was a problem with the request.'+reqObj.status); //else alert warnning
        }
  
    }
 
}
   
   
posted @ 2007-05-31 11:48  ZetaChow晓代码  阅读(333)  评论(0编辑  收藏  举报