﻿function MyAjax(url,callback,method)
{
    this.up_xhttp=null;
    this.up_url=url;
    this.callback=callback;
    this.method="GET";
    this.postcontent="";
    if(method)
        this.method=method;
}

MyAjax.prototype.Request=function()
{
    if (typeof(XMLHttpRequest) != 'undefined')
		this.up_xhttp = new XMLHttpRequest();
	else if (typeof(ActiveXObject) != 'undefined')
		this.up_xhttp = new ActiveXObject('Microsoft.XMLHTTP');
	else
	{
	    alert("创建XMLHttpRequest组件失败！");
	    return;
	}
	
    
    var _this=this;
    window.setTimeout(function(){_this.DoLoadPage()},100);
}


MyAjax.prototype.DoLoadPage=function()
{
    var _this=this;
    this.up_xhttp.onreadystatechange =function()
    {
        _this.OnProgressResult();
    }
    this.up_xhttp.open(this.method, this.up_url, true);
    if(this.method=="POST") 
        this.up_xhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	
	this.up_xhttp.send(this.postcontent);
	
	/**
	 * Firefox <= 2.0.0 doesn't fire onreadystatechange for synchronous requests.
	 * See http://lukav.com/wordpress/2007/04/12/firefox-firebug-and-synchronos-calls-problem/
	 */
	var isGecko = (document.addEventListener) ? true : false;
	try {
		if (isGecko && this.up_xhttp.onreadystatechange == null) {
		    this.callback(this.up_xhttp.responseText);			
		}
	}
	catch (e) { alert(e); }
};

MyAjax.prototype.OnProgressResult=function()
{
    if (this.up_xhttp.readyState == 4)
    {    
        if (this.up_xhttp.status == 200)    
            this.callback(this.up_xhttp.responseText);	  
        else
            alert('发生错误：'+this.up_xhttp.status);
    }
};


