日期:2014-05-16  浏览次数:20564 次

Ajax小记
使用Ajax的简单函数
var xmlhttp;   

function useAjax(url, func, data) {   
       
    if (window.XMLHttpRequest) {   
        xmlhttp = new XMLHttpRequest();   
    } else {   
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");   
    }
    
    xmlhttp.onreadystatechange = func;
    xmlhttp.open("POST", url, true);
    
    //Send the proper header information along with the request
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.setRequestHeader("Content-length", data.length);
	xmlhttp.setRequestHeader("Connection", "close");

    xmlhttp.send(data);
}   
  
function asynVisit(url) {
	var data = "name=smith&sex=male";
	
    useAjax(url, function() {   
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {   
            alert(xmlhttp.responseText);   
        }   
    }, data);   
}