ajax无翻页刷新简单实例

本文介绍了一个简单的示例,演示了如何利用Ajax从前端页面调用后台进行两个数相加的操作,并实时显示结果。该示例包括前端HTML页面和后端处理程序两部分。

1,前台页面:

<!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 type="text/javascript">
  var xmlHttp;
 
  function createXMLHttpRequest()
  {
    if(window.ActiveXObject)
    {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else if(window.XMLHttpRequest)
    {
        xmlHttp = new XMLHttpRequest();
    }
  }
 
  function addNumber()
  {
    createXMLHttpRequest();
    var url= "Handler.ashx?Num1="+document.getElementById("num1").value+"&Num2="+document.getElementById("num2").value;
    xmlHttp.open("GET",url,true);
    xmlHttp.onreadystatechange=showResult;
    xmlHttp.send(null);
  }
 
  function showResult()
  {
    if(xmlHttp.readyState==4)
    {
        if(xmlHttp.status==200)
        {
            document.getElementById("result").value=xmlHttp.responseText;
        }
     }
  }
  </script>
</head>

<body>


    <div>
    <input type="text" id="num1" value="7" /><br />
    <input type="text" id="num2" value="8"/><br />
    <input type="text" id="result" /><br />

    <input type="button" name="fdfds" value="计算" onclick="addNumber();" />

    </div>
</body>
</html>

2,计算页面:

<%@ WebHandler Language="C#" Class="Handler" %>

using System;
using System.Web;
using System.Text;


public class Handler : IHttpHandler {
   
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        int result = Convert.ToInt32(context.Request.QueryString["Num1"]) + Convert.ToInt32(context.Request.QueryString["Num2"]);
        context.Response.Write(result);
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}

转载于:https://www.cnblogs.com/xgcblog/archive/2011/08/19/2145748.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值