js ajax 函数

/*ajax对象创建*/
var xmlHttp;
var url;
var action;
function createXMLHttpRequest(){
    if(window.ActiveXObject){
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }else if(window.XMLHttpRequest){
        xmlHttp=new XMLHttpRequest();
    }
}
function sendXMLHttpRequest(){
    createXMLHttpRequest();
    xmlHttp.onreadystatechange=function(){
        if(xmlHttp.readyState==4){
            if(xmlHttp.status==200){
                if ('string' == typeof action){
                    eval(action);
                }else if ('function' == typeof action){
                    action(xmlHttp.responseText);
                }
            }else{
                alert("发生错误!");
            }
        }
    };
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);

}

例子:

function getcity(id){
    url="ajax.php?action=getcity&id="+id;
    action='document.getElementById("cityHTML").innerHTML=xmlHttp.responseText';
    sendXMLHttpRequest();
}

你可能感兴趣的:(Ajax,function,String,url,XMLhttpREquest,action)