Ajax基础(XMLHttpRequest对象)回顾

今天无聊,又回过头来写写 XMLHttpRequest对象直接访问Server.

js代码

var XHR,XmlDoc;

//创建XHR
function CreateXHR()
{
    if(window.XMLHttpRequest)
    {
        //非IE
        XHR = new window.XMLHttpRequest();
    }
    else if(window.ActiveXObject)
    {
        //IE
        XHR = new ActiveXObject("Microsoft.XMLHTTP");
    }
}

function CreateTalbeData()
{
    CreateXHR();
    XHR.onreadystatechange = GetCreateTalbeData;
    XHR.open("GET","Ajax.xml",true);
    XHR.send(null);
}

function GetCreateTalbeData()
{
    if(XHR.readystate == 4)
    {
        if(XHR.status == 200)
        {
            XmlDoc = XHR.responseXml;//XML Dom
           
            var Users = XmlDoc.getElementsByTagName("User");
          
            var Result = "<table width='100%' border='1' cellspacing='2' cellpadding='2'>";
            for(var i = 0 ;i < Users.length;i++)
            {
                Result + "<tr>";
                var u = Users[i];
                var UserName = u.getElementsByTagName("UserName")[0].firstChild.nodeValue;
                var Age = u.getElementsByTagName("UserName")[0].getAttribute("age");
                var Sex = u.getElementsByTagName("Sex")[0].firstChild.nodeValue;
                var Telphone = u.getElementsByTagName("Telphone")[0].firstChild.nodeValue;
               
                Result += "<td>" + UserName + "</td>";
                Result += "<td>" + Sex + "</td>";
                Result += "<td>" + Telphone + "</td>";              
               
                Result += "</tr>";         
            }
           
            Result += "</table>";
           
            document.all.divS.innerHTML = Result;    

            //此处应该用脚本创建元素构造,我偷懒了,呵呵!

        }
    }
}

页面文件

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Ajax.aspx.cs" Inherits="Ajax" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
    <script language="javascript" type="text/javascript" src="Scripts/Ajax.js">
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <input id="Button3" type="button" value="创建表格" οnclick="CreateTalbeData();" />       
        <div id="divS" style="background-color:Lime">
         </div>
    </div>
    </form>
</body>
</html>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值