本文摘自 http://www.codingsky.com/blog?blogid=404
习惯了用框架进行ajax访问?可别忘了ajax访问的基本代码与过程,本文让我们体验一下,不使用框架的最简单的进行ajax请求的方法。
var xmlHttpRequest; //XMLHttpRequest对象名
function sendRequest() { //发送http请求
getXMLHttpRequest(); //获得一个XMLHttpRequest对象到xmlHttpRequest
xmlHttpRequest.onreadystatechange = stateChange; //http状态变化时执行的操作
xmlHttpRequest.open("GET", "simpleAjax.xml"); //连接
xmlHttpRequest.send(null); //向服务器发送请求(这里内容为空)
}
function getXMLHttpRequest() {
if(window.ActiveXObject) { //IE类
xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest) { //Chrome类
xmlHttpRequest = new XMLHttpRequest();
}
}
function stateChange() {
if(xmlHttpRequest.readyState == 4 && xmlHttpRequest.status == 200) { //请求已完成且状态OK
alert(xmlHttpRequest.responseText); //作出的反应
}
}