ajax 原理部分js代码

/**
 * @author Ma Li
 
*/


/**
 * create a xmlHttpRequest object for other function
 * using
 
*/


var  xmlHttp  =  createXmlHttpRequestObject();

function  createXmlHttpRequestObject()
{
    
// an xmlHttpRequest object
    var xmlHttp;
    
    
// browser is IE
    if (window.ActiveXObject)
    
{
        
try
        
{
            
// create the ActiveX object
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");

        }

        
catch (e)
        
{
            
// exception throwed
            xmlHttp = false;

        }


    }

    
// browser is FireFox or OPERA or others
    else
    
{
        
try
        
{
            
// create the xmlHttpRequest object
            xmlHttp = new XMLHttpRequest();

        }

        
catch (e)
        
{
            
// exception throwed
            xmlHttp = false;

        }


    }

    
    
if (!xmlHttp)
    
{
        
// Cann't create object
        alert("Error creating the XMLHttpRequest object.");

    }

    
else
    
{
        
// return the object
        return xmlHttp;

    }

}


/**
 * the user event trigger
 
*/

function  process()
{
    
// xmlHttp object is not busy, processing my request
    if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
    
{
        
// get the user name from the form textfield
        name = encodeURIComponent(document.getElementById("myName").value);
        
// run the server script
        xmlHttp.open("GET""quickstart.php?name="+name, true);
        
// set the call back function
        xmlHttp.onreadystatechange = handleServerResponse;
        
// send the request
        xmlHttp.send(null);

    }

    
else
    
{
        
// failed, then try again after 1 second
        setTimeout("process()"1000);

    }


}


/**
 * the call back function
 
*/

function  handleServerResponse()
{
    
// processing complete
    if (xmlHttp.readyState == 4)
    
{
        
// processing successful
        if (xmlHttp.status == 200)
        
{
            
// get the XML document from the server
            xmlResponse = xmlHttp.responseXML;
            
// get the root element of the XML document
            xmlDocumentElement = xmlResponse.documentElement;
            
// get the first child of root eleemnt text content
            helloMessage = xmlDocumentElement.firstChild.data;
            
// display the message in the div tag
            document.getElementById("divMessage").innerHTML = 
                
'<i>' + helloMessage + '</i>';
            
// restart another process
            setTimeout("process()"1000);

        }

        
else
        
{
            
// processing error
            alert("there was a problem accessing the server: " + xmlHttp.statusText);

        }


    }


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值