Ajax与Atlas(1) 一个简单的Ajax实现

本文介绍了一个使用ASP.NET获取服务器内存状态并结合Ajax技术实时更新网页显示的应用案例。通过编写特定的ASP.NET页面和HTML文件,实现了每秒自动刷新当前服务器可用内存的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.新建一个Asp.net工程,新建一个GetServerMemory.aspx,拷入一下代码:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Diagnostics;

public partial class GetServerMemory : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    {
        PerformanceCounter myMemory 
= new PerformanceCounter();
        myMemory.CategoryName 
= "Memory";
        myMemory.CounterName 
= "Available KBytes";

        
string txtResult = "-->当前可用内存:" + myMemory.NextValue().ToString() + "KB";
        Response.Write(DateTime.Now.ToLongTimeString() 
+ txtResult);
    }
}
这个页面的作用是用来获得当前的服务器内存使用状况,并Response一个字符串

2.新建一个Test.html,拷入一下代码
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    
<title>Untitled Page</title>
    
<script language="javascript">
        
var objXmlHttp = new ActiveXObject("Microsoft.XMLHttp");
        
        
function sendAjaxRequest()
        {
            objXmlHttp.Open(
"Post","GetServerMemory.aspx",true);
            objXmlHttp.send(
null);
            objXmlHttp.onreadystatechange 
= showResult;
        }
        
        
function showResult()
        {
            
if(objXmlHttp.readystate == 4 || objXmlHttp.readystate == "complete")
            {
                document.getElementById(
'divResult').innerHTML = objXmlHttp.responsetext;
            }
        }
        
        setInterval(
'sendAjaxRequest()',1000);
    
</script>
</head>
<body>
    
<div id="divResult"></div>
</body>
</html>

试一下效果如何 ^_^

Ajax的核心思想是使用javascript异步获取服务器数据,并刷新页面显示,来增强客户体验,当页面中仅有一小部分数据需要及时刷新时,可以用到Ajax技术,Ajax可以说是一个锦上添花的东西,所以在程序编写时,不要为了Ajax而Ajax,一定要分析这样会带来什么好处。
www.mso.com.cn使用了Ajax,来进行比分直播,效果可以说非常之酷,这就是很好的使用了Ajax.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值