ajax 无刷新 显示
2015-09-30 14:00
351 查看
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace HTML5
{
/// <summary>
/// Summary description for Sum
/// </summary>
public class Sum : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
int a = Convert.ToInt32(context.Request.QueryString["num1"]);
int b = Convert.ToInt32(context.Request.QueryString["num2"]);
int result = a + b;
context.Response.Write(result);
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
<html>
<head>
<script language=jscript>
var xmlHttp;
function createXMLHttpRequest()
{
if(window.ActiveXObject)
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest)
{
xmlHttp = new XMLHttpRequest();
}
}
function AddNumber()
{
createXMLHttpRequest();
var url= "http://localhost/agv/Sum.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("sum").value=xmlHttp.responseText;
}
}
}
</script>
</head>
<body>
<div style="text-align: center">
<br />无刷新求和示例<br />
<br />
<input id="num1" style="width: 107px" type="text" onkeyup="AddNumber();" value="0" />
+<input id="num2" style="width: 95px" type="text" onkeyup="AddNumber();" value="0" />
=<input id="sum" style="width: 97px" type="text" /></div>
</body>
</html>
//**************************************************************
protected void Page_Load(object sender, EventArgs e)
{
string strName = HttpContext.Current.Request.QueryString["name"];
string strRes = "服务器返回的响应信息:\r\n" +
"Hello, " + strName + "!";
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Write(strRes);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
}
.ashx 文件用于写web handler的。其实就是带HTML和C#的混合文件。
同样.aspx也可以代替他的,不过在使用.aspx的时候必须得
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
不然返回的数据出了你需要的数据外,它还把它自身的html码和一起返回了.
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace HTML5
{
/// <summary>
/// Summary description for Sum
/// </summary>
public class Sum : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
int a = Convert.ToInt32(context.Request.QueryString["num1"]);
int b = Convert.ToInt32(context.Request.QueryString["num2"]);
int result = a + b;
context.Response.Write(result);
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
<html>
<head>
<script language=jscript>
var xmlHttp;
function createXMLHttpRequest()
{
if(window.ActiveXObject)
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest)
{
xmlHttp = new XMLHttpRequest();
}
}
function AddNumber()
{
createXMLHttpRequest();
var url= "http://localhost/agv/Sum.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("sum").value=xmlHttp.responseText;
}
}
}
</script>
</head>
<body>
<div style="text-align: center">
<br />无刷新求和示例<br />
<br />
<input id="num1" style="width: 107px" type="text" onkeyup="AddNumber();" value="0" />
+<input id="num2" style="width: 95px" type="text" onkeyup="AddNumber();" value="0" />
=<input id="sum" style="width: 97px" type="text" /></div>
</body>
</html>
//**************************************************************
protected void Page_Load(object sender, EventArgs e)
{
string strName = HttpContext.Current.Request.QueryString["name"];
string strRes = "服务器返回的响应信息:\r\n" +
"Hello, " + strName + "!";
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Write(strRes);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
}
.ashx 文件用于写web handler的。其实就是带HTML和C#的混合文件。
同样.aspx也可以代替他的,不过在使用.aspx的时候必须得
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
不然返回的数据出了你需要的数据外,它还把它自身的html码和一起返回了.
相关文章推荐
- 【Loadrunner】将乱码转译为中文方法
- Access restriction: The method encode(byte[]) from the type CharacterEncoder,Access restriction: Th
- iOS 8 自动布局sizeclass和autolayout的使用
- Python3的一些用法
- TCP状态迁移,CLOSE_WAIT & FIN_WAIT2 的问题
- 敏感词过滤
- [LeetCode 174] Dungeon Game
- JAVA学习
- 华为P7如何官方解锁
- hive中的全排序
- 如何更好地理解和使用Github
- Android Studio 中ADB WIFI 插件的使用
- mysql查看和删除唯一索引
- 【Python3】error: Microsoft Visual C++ 10.0 is required 错误解决办法
- linux argument list too long的解决办法
- 从图片中的一点取色
- 顺序表整体删除某值或某一区间
- Android Studio安装后提示No JVM installation found解决办法
- win7jdk环境变量配置
- use rbenv replace rvm