`
luozhaoyu
  • 浏览: 343176 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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);   
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics