在一个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 ...
随机推荐
- 20170103简单解析MySQL查询优化器工作原理
转自博客http://www.cnblogs.com/hellohell/p/5718238.html 感谢楼主的贡献 查询优化器的任务是发现执行SQL查询的最佳方案.大多数查询优化器,包括MySQL ...
- Apache问题处理服务器访问不了
1. 查看apache的错误日志 我的apache日志文件目录 /var/log/httpd/error.log [Sun Nov 20 21:17:24 2016] [error] server r ...
- Redis集群(八):Redis Sharding集群
一.Redis目前的集群方案主要有两种:Redis Sharding和Redis Cluster 1.Redis Sharding:3.0以前基本上使用分片实现集群,目前主流方案,客户端实现 2.Re ...
- caffe添加自己的层
首先修改src/caffe/proto/下的caffe.proto,修改好后需要编译 然后修改include/caffe/layers/logwxl_layer.hpp 然后修改src/caffe/l ...
- KMP算法实现
链接:http://blog.csdn.net/joylnwang/article/details/6778316 KMP算法是一种很经典的字符串匹配算法,链接中的讲解已经是很明确得了,自己按照其讲解 ...
- Sql 2008 的常用函数
1.LEN 函数:返回数据的长度 ') 返回:8 2.ASCII函数:返回字符串最左边的ascii值 SELECT ASCII('abc') 返回:97 3.LEFT函数:从左边开始截取指定长度的字符 ...
- SimpleDateFormat转换时间格式
SimpleDateFormat有两个常用的方法parse和format 其中SimpleDateFormat在创建时有一下集中格式可以选择 SimpleDateFormat sdf = new Si ...
- Mysql 修改字段默认值
环境:MySQL 5.7.13 问题描述:建表的时候,users_info表的role_id字段没有默认值,后期发现注册的时候,需要提供给用户一个默认角色,也就是给role_id字段一个默认值. 当前 ...
- pycharm5注册码
43B4A73YYJ-eyJsaWNlbnNlSWQiOiI0M0I0QTczWVlKIiwibGljZW5zZWVOYW1lIjoibGFuIHl1IiwiYXNzaWduZWVOYW1lIjoiI ...
- easyUI 如何不跳转页面,只是加载替换center部分内容
以前做的一个故障报修系统,前端框架使用easyUI框架,layout布局,center使用datagrid .点击左边树形菜单时时页面跳转,想要知道如何点击菜单时不进行页面跳转,而是只对center模 ...