上一步简单的使用了XMLHTTPRequest对象,这次来换个花样,写一下有点样子的代码。

HTML代码,也就是.aspx的,也可以是.html


    <title>无标题页</title>
    
<script language="JavaScript" type="text/javascript" >
    
var http_request=false;
    
function send_request(url)
    {
        
        
//初始化、指定处理函数、发送请求的函数
        http_request=false;
        
//开始初始化XMLHttpRequest对象
        if(window.XMLHttpRequest)
        {
            
            
//火狐等浏览器
            http_request=new XMLHttpRequest();
            
if(http_request.overrideMimeType)
            {
//设置MIME类别
                http_request.overrideMimeType('text/xml');
            }
        }
        
else if(window.ActiveXObject)
        {
            
            
//IE浏览器
            try
            {
                http_request
=new ActiveXObject("Msxml2.XMLHTTP");
            }
            
catch (e)
            {
                
try
                {
                    http_request
=new ActiveXObject("Microsoft.XMLHTTP");    
                }
                
catch (e)
                {}
            }
        }
            
if(!http_request)
            {
                
//异常,创建对象实例失败
                window.alert("不能创建XMLHttpRequest对象实例.");
                
return false;
            }
            http_request.onreadystatechange
=processRequest;            
            http_request.open(
"GET",url,true);
            http_request.send(
null);            
        
    }
    
//处理返回信息的函数
    function processRequest()
    {
        
if(http_request.readyState==4)
        {
            
//判断对象状态
            if(http_request.status==200)
            {
                
//信息已经成功返回,开始处理信息
                document.getElementById(currentPos).innerHTML=http_request.responseText;
                
            }
            
else
            {
                
//页面不正常
                alert("您所请求的页面有异常.");
            }
        }
    }
    
    
//显示部门下的岗位
    function showRoles(obj)
    {
        document.getElementById(obj).parentNode.style.display
="";
        document.getElementById(obj).innerHTML
="正在读取数据我的AJAX第二步";
        currentPos
=obj;
        send_request(
"Test2.aspx?playPos="+obj);
    }




</script>
</head>
<body>
    
<table width="200" border="0" cellspacing="0" cellpadding="0">
        
<tr>
            
<td height="20">
                
<href="javascript:void(0)" onclick="showRoles('pos_1')">经理室</a>
            
</td>
        
</tr>
        
<tr style="display:none">
            
<td height="20" id="pos_1">&nbsp;</td>
        
</tr>
        
<tr>
            
<td height="20">
                
<href="javascript:void(0)" onclick="showRoles('pos_2')">开发部</a>
            
</td>
        
</tr>
        
<tr style="display:none">
            
<td id="pos_2" height="20">&nbsp;</td>
        
</tr>
    
</table>
</body>
</html>

 

.aspx.cs的代码,注意,下面的代码和上面的HTML代码不是一个页面

 Test2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
         
string playPos=Request.Params["playPos"];
         
if (playPos == "pos_1")
         {
             Response.Write(
"&nbsp;&nbsp;总经理<br/>&nbsp;&nbsp;副总经理");
         }
         
else if(playPos=="pos_2")
         {
            Response.Write(
"&nbsp;&nbsp;总工程师<br/>&nbsp;&nbsp;软件工程师");
         }
    }
}

 

相关文章:

  • 2021-12-17
  • 2022-12-23
  • 2022-12-23
  • 2021-06-25
  • 2021-09-09
  • 2021-07-07
  • 2021-11-03
猜你喜欢
  • 2022-01-29
  • 2021-07-25
  • 2021-10-29
  • 2022-02-09
  • 2021-07-24
  • 2021-09-23
  • 2022-12-23
相关资源
相似解决方案