一个简单的Ajax程序

    技术2022-05-11  82

     

    < html > < head > < title > 显示内存信息 </ title > < script  language =javascript > //处理HttpRequest,返回一个HTTPRequest//var req;function getXMLHTTPRequest(){      var xmlHttp=null;    try    {            // Firefox, Opera 8.0+, Safari            xmlHttp=new XMLHttpRequest();        }    catch (e)    {            // Internet Explorer            try        {                  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");              }        catch (e)        {                  try            {                        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");                    }            catch (e)            {                        alert("Your browser does not support AJAX!");                    }             }        }          return xmlHttp;}//发送请求function sendRequest(url,params,HttpMethod){    if (!HttpMethod)    {        HttpMethod="POST";    }    var req=getXMLHTTPRequest();//1、得到XMLHTTPRequest    if (req!=null)    {        req.open(HttpMethod,url,true);//2、Open                if ( HttpMethod =="POST")//3、Post的头文件        {            req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");            }                req.send(params);//4、send                //5、监视        req.onreadystatechange = function()        {            //6、返回值时的操作            if (req.status == 200//200表示成功,404表示未找到            {                    if ( req.readystate==4 || req.readystate=='complete' )                    {                    //alert( req.responsetext );                                        //document.getElementById('show').innerHTML =req.responsetext;                    var doc = document.createElement("div");                                        doc.innerHTML =req.responsetext;//返回的是一个xml或者用Response.Write写的内容                    //createTextNode增加一个Text节点                    //var txt = document.createTextNode( req.responsetext );                    //doc.appendChild( txt );                    document.getElementById('show').appendChild( doc );                }            }        };    }}function sendAJAX(){    sendRequest("WebForm1.aspx",null,"POST");//调用XMLHTTPRequest    }setInterval('sendAJAX()',1000);//每1秒调用一次sendAJAX() </ script > </ head > < body  >      < div  id ="show" ></ div > </ body > </ html >

     

    保存为Client.Html

     

    using  System; using  System.Collections; using  System.ComponentModel; using  System.Data; using  System.Drawing; using  System.Web; using  System.Web.SessionState; using  System.Web.UI; using  System.Web.UI.WebControls; using  System.Web.UI.HtmlControls; using  System.Diagnostics;     // 命名空间 namespace  AjaxTest {    /// <summary>    /// WebForm1 的摘要说明。    /// </summary>    public class WebForm1 : System.Web.UI.Page    {        private void Page_Load(object sender, System.EventArgs e)        {            // 在此处放置用户代码以初始化页面            PerformanceCounter perform = new PerformanceCounter();            perform.CategoryName = "Memory";//内存            perform.CounterName = "Available KBytes";            string txtResult = "-->当前可用内存:" +perform.NextValue().ToString() + "KB";            Response.Write(DateTime.Now.ToLongTimeString() + txtResult);        }        Web 窗体设计器生成的代码    }}

     

    保存为WebForm1.aspx

     


    最新回复(0)