知行合一

享受过程 漠视结果

导航

ajax手写代码实现

    <script type = "text/javascript">
    
var xmlhttp = null;
    
try
    {
        xmlhttp 
= new ActiveXObject("Microsoft.XMLHTTP");
    }
    
catch(e)
    {
        
try
        {
            xmlhttp 
= new XMLHttpRequest();
        }
        
catch(e)
        {
            alert(
"xmlhttp创建失败");
        }
    }
    
    
function state_Change()
    {
        
if(xmlhttp.readyState == 4)
        {
            
if(xmlhttp.status == 200)
            {
                
var mes = xmlhttp.responseText;
                document.getElementById(
"t1").innerText=mes;
            }
        }
    }
    
    
function GetMes()
    {
        
//异步
        xmlhttp.open("POST" ,"Default.aspx",true);
        xmlhttp.onreadystatechange
=state_Change;
        xmlhttp.send(
null);

        
        
//同步
        xmlhttp.open("POST","Default.aspx",false);
        xmlhttp.send(
null);
        
var mes = xmlhttp.responseText;
        document.getElementById(
"t1").innerText=mes;
        
        setTimeout(GetMes,
1000);
    }
    
    GetMes();
    
</script>

posted on 2011-05-14 13:33  项羽  阅读(443)  评论(0编辑  收藏  举报