/*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);
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();
}