﻿function ajax()
{
    this.xmlHttp=null;
    this.customHandleRequest=function(){};
    
    this.createRequestXmlHttp=function()
    {
        //开始初始化XMLHttpRequest对象  
        if(window.XMLHttpRequest) { //Mozilla 浏览器  
            xmlHttp = new XMLHttpRequest();   
            if (xmlHttp.overrideMimeType) {//设置MiME类别  
                xmlHttp.overrideMimeType('text/xml');   
            }   
        }   
        else if (window.ActiveXObject) { // IE浏览器  
            try {   
                xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");   
            } catch (e) {   
                try {   
                    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");   
                } catch (e) {}   
            }   
        }   
        if (!xmlHttp) { // 异常，创建对象实例失败  
            window.alert("不能创建XMLHttpRequest对象实例.");   
        }   
    }
    
    /****************************************************************
    pageUrl：请求的路径
    methodName：请求的方法名
    param：发送的参数
    method：指明是“GET”还是“POST”
    async：指明同步进行还是异步进行，true为异步进行
    ****************************************************************/
    this.callServerNoBack=function(pageUrl,methodName,param,method,async)
    {
        this.createRequestXmlHttp();
        //xmlHttp.onreadystatechange=this.handleRequest;
        if(method.toUpperCase()=="GET")
        {  
            xmlHttp.open("GET",pageUrl+"/"+methodName+"?"+param,async);
            xmlHttp.send(null);
        }
        else
        {
            xmlHttp.open("POST",pageUrl+"/"+methodName,async);
            xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
            xmlHttp.send(param);
        }
    }
    
    /****************************************************************
    pageUrl：请求的路径
    methodName：请求的方法名
    param：发送的参数
    method：指明是“GET”还是“POST”
    async：指明同步进行还是异步进行，true为异步进行
    ****************************************************************/
    this.callServerAndBack=function(pageUrl,methodName,param,method,async)
    {
        this.createRequestXmlHttp();
        xmlHttp.onreadystatechange=this.handleRequest;
        if(method.toUpperCase()=="GET")
        {  
            xmlHttp.open("GET",pageUrl+"/"+methodName+"?"+param,async);
            xmlHttp.send(null);
        }
        else
        {
            xmlHttp.open("POST",pageUrl+"/"+methodName,async);
            xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
            xmlHttp.send(param);
        }
    }
    
    /****************************************************************
    pageUrl：请求的路径
    methodName：请求的方法名
    param：发送的参数
    method：指明是“GET”还是“POST”
    async：指明同步进行还是异步进行，true为异步进行
    ****************************************************************/
    this.callServerCustomBack=function(pageUrl,methodName,param,method,async)
    {
        this.createRequestXmlHttp();
        xmlHttp.onreadystatechange=this.customHandleRequest;
        if(method.toUpperCase()=="GET")
        {  
            xmlHttp.open("GET",pageUrl+"/"+methodName+"?"+param,async);
            xmlHttp.send(null);
        }
        else
        {
            xmlHttp.open("POST",pageUrl+"/"+methodName,async);
            xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
            xmlHttp.send(param);
        }
    }
    
    this.handleRequest=function()
    {
        if(xmlHttp.readyState==4)
        {
            if(xmlHttp.status==200)
            {
                alert(xmlHttp.responseText);
            }
        }
    }
}