支持自定义回调函数的异步调用

    做一个最简单的POST方式异步调用的请求,支持自定义的回调函数,该回调函数获取异步请求返回的XMLDOM对象,代码如下:

function postRequest(url,parameters,callBack){
    
var xmlHttp = getXmlHttp(); //create xmlHttpRequest
    if(xmlHttp !=null){
        xmlHttp.onreadystatechange 
= function(){
            
if(xmlHttp.readyState == 4){
                
if(xmlHttp.status == 200){
                    xmlDom 
= getXmlDom(xmlHttp.responseText);//create xmlDom
                    
if(xmlDom != null){
                        eval(callBack(xmlDom));
                    }
                }
            }
        }
        xmlHttp.open(
'Post',url,true);
        xmlHttp.setRequestHeader(
"Content-Length",parameters.length);
        xmlHttp.setRequestHeader(
"Content-Type","application/x-www-form-urlencoded");
        xmlHttp.send(parameters);
    }
}

posted on 2008-03-15 13:52  Bean.Hsiang  阅读(735)  评论(0编辑  收藏  举报