xml(标准化越来越近了)HttpRequest是ajax(动态网站静态化)的基础对象。异步的数据请求是通过这个对象来实现的。下面的代码是建立xml(标准化越来越近了)HttpRequest对象的示例 。
代码在IE6、FireFox1.5、NetScape8.1、Opera8.54调试通过。服务器为Window2000 + IIS5
1、创建xml
(标准化越来越近了)HTTPREQUEST对象
var xhr;
var requestType = "";
//xhr = new xml
(标准化越来越近了)HttpRequest();
function createxml
(标准化越来越近了)HttpRequest()
{
if (window.ActiveXObject) // IE下创建xml
(标准化越来越近了)HTTPREQUEST
{
xhr = new ActiveXObject("Microsoft.xml
(标准化越来越近了)HTTP");
}
else if (window.xml
(标准化越来越近了)HttpRequest) // 其他浏览器创建xml
(标准化越来越近了)HTTPREQUEST
{
xhr = new xml
(标准化越来越近了)HttpRequest();
}
}
这种方法对于低版本的IE不适合。
2、xml
(标准化越来越近了)HTTPREQUEST对象请求数据
function startRequest(requestedList)
{
if (xhr)
{
requestType = requestedList;
createxml
(标准化越来越近了)HttpRequest();
xhr.onreadystatechange = handleStateChange;
xhr.open("GET","../ajax
(动态网站静态化)/parasexml
(标准化越来越近了).xml
(标准化越来越近了)",true);
xhr.send(null);
}
else
alert("xml
(标准化越来越近了)HTTPREQUEST IS FALSE");
}
这是个处理xml
(标准化越来越近了)文档的示例,请求的文件是parasexml
(标准化越来越近了).xml
(标准化越来越近了)文件
这里需要说明的是:如果请求的是一个HTML文件,服务器对象会将所有的标签全部返回,包括<HTML>、<head>、<meta>等标签。响应数据如果包含HTML标签,最好把这些标签去掉。
3、xml
(标准化越来越近了)HTTPREQUEST对象返回数据处理
function handleStateChange()
{
if (xhr.readyState == 4)
{
if (xhr.status == 200)
{
if (requestType == "north")
{
listNorthStates();
}
if (requestType == "all")
{
listAllStates();
}
}
}
}
4、数据处理函数
function listNorthStates()
{
// xhr 为xml
(标准化越来越近了)HTTPREQUEST对象
// xml
(标准化越来越近了)Doc为xml
(标准化越来越近了)HTTPREQUEST响应的xml
(标准化越来越近了)文档对象
var xml
(标准化越来越近了)Doc = xhr.responsexml
(标准化越来越近了); // 取得xml
(标准化越来越近了)文档对象
var northNode = xml
(标准化越来越近了)Doc.getElementsByTagName("north")[0]; // 取得所有处于北方的节点
var northStates = northNode.getElementsByTagName("state"); // 在处于北方的节点中提取省份数据
outputList("north States", northStates); // 输出省份数据
}
function listAllStates()
{
var xml
(标准化越来越近了)Doc = xhr.responsexml
(标准化越来越近了);
var allStates = xml
(标准化越来越近了)Doc.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[i]; // 取得当前的省份
// 生成输出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 xml
(标准化越来越近了)ns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" CONTENT="text/html; charset=gb2312">
<meta http-equiv="Cache-Control" CONTENT="no-cache">
<meta http-equiv="Pragma" CONTENT="no-cache">
<meta http-equiv="Expires" CONTENT="0">
<title>ajax
(动态网站静态化) 测试</title>
<script type="text/javascript">
///// 创建xml
(标准化越来越近了)HttpRequest对象
var xhr;
var requestType = "";
//xhr = new xml
(标准化越来越近了)HttpRequest();
function createxml
(标准化越来越近了)HttpRequest()
{
if (window.ActiveXObject)
{
xhr = new ActiveXObject("Microsoft.xml
(标准化越来越近了)HTTP");
}
else if (window.xml
(标准化越来越近了)HttpRequest)
{
xhr = new xml
(标准化越来越近了)HttpRequest();
}
}
///// XML文档处理
function startRequest(requestedList)
{
if (xhr)
{
requestType = requestedList;
createxml
(标准化越来越近了)HttpRequest();
xhr.onreadystatechange = handleStateChange;
xhr.open("GET","../ajax
(动态网站静态化)/parasexml
(标准化越来越近了).xml
(标准化越来越近了)",true);
xhr.send(null);
}
else
alert("xml
(标准化越来越近了)HTTPREQUEST IS FALSE");
}
正文:
http://www.cncms.com.cn/ajax(动态网站静态化)/k291426.html
[1] [2] 下一页