在一个aspx或ashx页面里进行多次ajax调用
在用ajax开发asp.net程序里.利用ashx页面与前台页面进行数据交互.但是每个ajax交互都需要一个ashx页面.结果是项目里一大堆ashx页面.使项目难以管理.现在我们就想办法让一个ashx页面里允许多个ajax交互;
前台页面AjaxTest.htm,内容如下
<!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>
<title>本页用不同的方式与后台进行交互</title>
<script type="text/javascript" src="JS/jquery-1.4.2.min.js"></script>
<script type="text/javascript" >
//使用jquery库进行ajax交互
$(document).ready(function(){
//进行一个ajax请求,command告诉后台调用哪个方法
$.get("Handler.ashx",{command:"method1",value:"chentao"},function(data){
alert(data);
});
//进行一个ajax请求,command告诉后台调用method2方法
$.get("Handler.ashx",{command:"method2",value:"tangyu"},function(data){
alert(data);
})
</script>
</head>
<body>
</body>
</html>
后台建立一个Handler.ashx页面 内容如下
<%@ WebHandler Language="C#" class="Handler" %>
using System;
using System.Web;
public class Handler : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
if (context.Request["command"]!=null)
{
//得到前台传过来的command,确定调用哪个方法
string command = context.Request["command"].ToString();
string data = context.Request["value"].ToString();
switch (command)
{
case "method1":
method1(context);
break;
case "method2":
method2(context);
break;
default:
break;
}
}
}
public bool IsReusable {
get {
return false;
}
}
public void method1(HttpContext context)
{
context.Response.Write("hello,"+context.Request["value"].ToString());
}
public void method2(HttpContext context)
{
context.Response.Write("hello,"+context.Request["value"].ToString());
}
}
如果有多个方法,switch case里的判断将会很多.考虑用更简单的方法.使用反射
<%@ WebHandler Language="C#" class="Handler" %>
using System;
using System.Web;
public class Handler : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
if (context.Request["command"] != null)
{
//
string command = context.Request["command"].ToString();
System.Reflection.MethodInfo method = this.GetType().GetMethod(command);
if (method != null)
{
method.Invoke(this, new object[] { context});
}
}
}
public bool IsReusable {
get {
return false;
}
}
public void method1(HttpContext context)
{
context.Response.Write("hello"+context.Request["value"].ToString());
}
public void method2(HttpContext context)
{
context.Response.Write("hello,"+context.Request["value"].ToString());
}
}
使用反射大大简化了程序.
=====================================================
使用aspx页面与ajax交互
新建一个aspx页面 WebMethod.aspx
将WebMethod.aspx页里的多余部分删除,只保留
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="WebMethod.aspx.cs" Inherits="WebMethod" %>
这一条语句
WebMethod.aspx.cs内容如下
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.Services;
using System.Reflection;
public partial class WebMethod : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string methodName = HttpContext.Current.Request.PathInfo.Substring(1);
// Response.Write(methodName);
MethodInfo method = this.GetType().GetMethod(methodName);
if (method != null)
{
Response.Write(method.Invoke(this,new object[]{}));
}
// Response.Write(GetResult());
}
[WebMethod(EnableSession=true)]
public string GetResult()
{
//return "hello";
if (HttpContext.Current.Request["name"] != null)
{
string value = HttpContext.Current.Request["name"].ToString();
//HttpContext.Current.Request.PathInfo;
return "{'name':'"+value+"'}";
}
else
{
return "{name:'error'}";
}
}
}
test.html页面与WebMethod.aspx页面进行ajax交互 test.html页面内容
<!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>
<title>使用aspx页面进行交互</title>
<script type="text/javascript" src="JS/jquery-1.4.2.min.js"></script>
<script type="text/javascript" >
$(document).ready(function(){
$.ajax({
type: "POST",
url: "WebMethod.aspx/GetResult",
data: "name=chentao",
dataType: "text",
success: function(d){
alert(d);
}
});
});
</script>
</head>
<body>
</body>
</html>
在一个aspx或ashx页面里进行多次ajax调用的更多相关文章
- 在小程序中修改上一个页面里data中的数据调用上一个页面的方法
//获取已经打开的页面的数组 var pages = getCurrentPages(); //获取上一个页面的所有的方法和data中的数据 var lastpage = pages[pages.l ...
- asp.net web 项目 针对aspx和ashx的 IHttpHandlerFactory 开发
ASP.NET Framework处理一个Http Request的流程: HttpRequest-->inetinfo.exe-->ASPNET_ISAPI.dll-->ASPNE ...
- jquery.ajax请求aspx和ashx的异同 Jquery Ajax调用aspx页面方法
1.jquery.ajax请求aspx 请求aspx的静态方法要注意一下问题: (1)aspx的后台方法必须静态,而且添加webmethod特性 (2)在ajax方法中contentType必须是“a ...
- Jquery Ajax调用aspx页面方法
Jquery Ajax调用aspx页面方法 在asp.net webform开发中,用jQuery ajax传值一般有几种玩法 1)普通玩法:通过一般处理程序ashx进行处理: 2)高级玩法:通过as ...
- ASP.NET前台html页面AJAX提交数据后台ashx页面接收数据
摘要:最近在写网站,好不容易弄好了需求又变了,没错企业的门户网站硬要弄成后台管理系统一样,没办法作为小工的我只能默默的改.前台HTML页面需要提交数据到后台处理,又不能用form表单,于是乎研究了1天 ...
- 项目中Ajax调用ashx页面中的Function的实战
前台页面: 使用几个display=none的空间存储DropdownList中的值,点击Search Button后刷新页面再次给DropdownList赋值使用 <%@ Page Langu ...
- aspx、ashx、asmx文件处理请求效率比较
人生总是面临着许多抉择许多困惑!作为一名“攻城师”或“程序猿”的我们,工作的时候更是如此.你曾经是否苦恼过在系统中使用哪种文件编写客户端请求最合适或最高效呢?aspx.ashx.asmx到底该如何选择 ...
- aspx与ashx
ashx在VS的中文版是新建“一般处理程序”,其实是一个实现类System.Web.IHttpHandler接口的类.而任何一个实现了IHttpHandler接口的类都能作为一个外部请求的目标程序.H ...
- 人生的抉择—aspx、ashx、asmx文件处理请求效率比较
人生总是面临着许多抉择许多困惑!作为一名"攻城师"或"程序猿"的我们,工作的时候更是如此.你曾经是否苦恼过在系统中使用哪种文件编写客户端请求最合适或最高效呢?a ...
随机推荐
- 常用Linux命令
1.mkdir 建立目录 $ mkdir testdir 2.ls 列出目录下的内容的详细信息 ls -al testdir 3.cd 更换当前工作目录 cd testdir 4.pwd ...
- Goodbye 2016 总结与展望
今天居然是2016年的最后一天了,写点什么回忆吧. 2016开始的时候我刚拿到普及组一等奖,还只是压线,水平很差.学校并不知道这有多差,于是狠狠宣传这所谓的"光荣事迹".那段时间我 ...
- RabbitMQ总结概念
AMQP:一个提供统一消息服务的应用层标准高级消息队列协议,是应用层协议的一个开放标准,为面向消息的中间件设计 http://www.diggerplus.org/archives/3110 AMQP ...
- Python学习--Python基础语法
第一个Python程序 交互式编程 交互式编程不需要创建脚本文件,是通过 Python 解释器的交互模式进来编写代码. linux上你只需要在命令行中输入 Python 命令即可启动交互式编程,提示窗 ...
- 让Visual Studio 2013为你自动生成XML反序列化的类
Visual Sutdio 2013增加了许多新功能,其中很多都直接提高了对代码编辑的便利性.如: 1. 在代码编辑界面的右侧滚动条上显示不同颜色的标签,让开发人员可以对所编辑文档的修改.查找.定位情 ...
- OpenStack从入门到放弃
OpenStack从入门到放弃 目录: 为何选择云计算/云计算之前遇到的问题 什么是云计算 云服务模式 云应用形式 传统应用与云感知应用 openstack及其相关组件介绍 flat/vlan/gre ...
- ES6深入学习记录(一)class方法相关
今天学习class相关的一些使用方法,着重在于class extends class之间可以通过extends关键字实现继承,这比ES5的通过修改原型链实现继承,要清晰和方便很多. 上面的代码定义了一 ...
- a new Poster
- 深入理解JS的闭包
闭包(closure)是Javascript语言的一个难点,也是它的特色,很多高级应用都要依靠闭包实现. 一.变量的作用域 要理解闭包,首先必须理解Javascript特殊的变量作用域. 变量的作用域 ...
- PHP基础之PDO
简介 PDO(PHP Data Object)是指PHP数据对象,它定义了一个轻量级的一致接口来统一操作各种数据库.PDO提供了一个数据访问抽象层,这意味着,不管使用哪种数据库,都可以用相同的函数(方 ...