1.新建一个Asp.net工程,新建一个GetServerMemory.aspx,拷入一下代码:
这个页面的作用是用来获得当前的服务器内存使用状况,并Response一个字符串
2.新建一个Test.html,拷入一下代码
试一下效果如何 ^_^
Ajax的核心思想是使用javascript异步获取服务器数据,并刷新页面显示,来增强客户体验,当页面中仅有一小部分数据需要及时刷新时,可以用到Ajax技术,Ajax可以说是一个锦上添花的东西,所以在程序编写时,不要为了Ajax而Ajax,一定要分析这样会带来什么好处。
www.mso.com.cn使用了Ajax,来进行比分直播,效果可以说非常之酷,这就是很好的使用了Ajax.
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);
}
}
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);
}
}
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>
<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.