function createXMLHttpRequest()
{
if (window.ActiveXObject) // IE下创建XMLHTTPREQUEST
{
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
else if (window.XMLHttpRequest) // 其他浏览器创建XMLHTTPREQUEST
{
xhr = new XMLHttpRequest();
}
}
这种方法对于低版本的IE不适合。
2、XMLHTTPREQUEST对象请求数据
function startRequest(requestedList)
{
if (xhr)
{
requestType = requestedList;
createXMLHttpRequest();
xhr.onreadystatechange = handleStateChange;
xhr.open("GET","../ajax/paraseXML.xml",true);
xhr.send(null);
}
else
alert("XMLHTTPREQUEST IS FALSE");
}
function handleStateChange()
{
if (xhr.readyState == 4)
{
if (xhr.status == 200)
{
if (requestType == "north")
{
listNorthStates();
}
if (requestType == "all")
{
listAllStates();
}
}
}
}
4、数据处理函数
function listNorthStates()
{
// xhr 为XMLHTTPREQUEST对象
// xmlDoc为XMLHTTPREQUEST响应的XML文档对象
var xmlDoc = xhr.responseXML; // 取得XML文档对象
var northNode = xmlDoc.getElementsByTagName("north")[0]; // 取得所有处于北方的节点
var northStates = northNode.getElementsByTagName("state"); // 在处于北方的节点中提取省份数据
outputList("north States", northStates); // 输出省份数据
}
function listAllStates()
{
var xmlDoc = xhr.responseXML;
var allStates = xmlDoc.getElementsByTagName("state"); // 取得所有的省份数据
outputList("All States in document ",allStates); // 输出省份的数据
}
/**********************************************************
// 输出数据
// title: 输出数据的标题
// states: 输出数据来源
********************************************************/
function outputList(title,states)
{
var out = title;
var currentState = null; // 初始化省份对象
for (var i = 0; i < states.length; i++) // 列出Ststes对象的所有数据,生成输出串
{
currentState = states; // 取得当前的省份
// 生成输出HTML字符串
out = out + "<ul><font face='仿宋_GB2312'><span style='font-size: 9pt'>";
out = out + "<li>" + currentState.childNodes[0].nodeValue + "</li>";
out = out + "</span></font></ul>";
}
// 取得输出串的对象,输出生成的字符串
var test = document.getElementById("test");
test.innerHTML = out;
}
5、完整示例文件
<!DOCTYPE html PUBLIC "-//W3C//DTD XHML 1.0 Strict//EN" "http://www.w3.org/TR/xHML/DTD/xhtml-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<title>AJAX 测试</title>
<script type="text/javascript">
///// 创建XMLHttpRequest对象
var xhr;
var requestType = "";
//xhr = new XMLHttpRequest();
function createXMLHttpRequest()
{
if (window.ActiveXObject)
{
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
else if (window.XMLHttpRequest)
{
xhr = new XMLHttpRequest();
}
}
///// XML文档处理
function startRequest(requestedList)
{
if (xhr)
{
requestType = requestedList;
createXMLHttpRequest();
xhr.onreadystatechange = handleStateChange;
xhr.open("GET","../ajax/paraseXML.xml",true);
xhr.send(null);
}
else
alert("XMLHTTPREQUEST IS FALSE");
}
function handleStateChange()
{
if (xhr.readyState == 4)
{
if (xhr.status == 200)
{
if (requestType == "north")
{
listNorthStates();
}
if (requestType == "all")
{
listAllStates();
}
}
}
}
function listNorthStates()
{
var xmlDoc = xhr.responseXML;
var northNode = xmlDoc.getElementsByTagName("north")[0];
var northStates = northNode.getElementsByTagName("state");
outputList("north States", northStates);
}
function listAllStates()
{
var xmlDoc = xhr.responseXML;
var allStates = xmlDoc.getElementsByTagName("state");
outputList("All States in document ",allStates);
}
function outputList(title,states)
{
var out = title;
var currentState = null;
for (var i = 0; i < states.length; i++)
{
currentState = states;
out = out + "<ul><font face='仿宋_GB2312'><span style='font-size: 9pt'>";
out = out + "<li>" + currentState.childNodes[0].nodeValue + "</li>";
out = out + "</span></font></ul>";
}
var test = document.getElementById("test");
test.innerHTML = out;
}
</script>