在一个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 ...
随机推荐
- C语言中函数的传入值与传出值
看到一个函数的原型后,怎么样一眼看出来哪个参数做输入哪个做输出? 函数传参如果传的是普通变量(不是指针)那肯定是输入型参数: 如果传指针就有 2 种可能性了,为了区别,经常的做法是: 如果这个参数是做 ...
- Leetcode分类刷题答案&心得
Array 448.找出数组中所有消失的数 要求:整型数组取值为 1 ≤ a[i] ≤ n,n是数组大小,一些元素重复出现,找出[1,n]中没出现的数,实现时时间复杂度为O(n),并不占额外空间 思路 ...
- 修改/etc/profile和/etc/environment导致图形界面无法登陆的问题
在使用ubuntu开发时,往往要修改PATH变量,有时会通过修改/etc/profile和/etc/environment来修改默认的PATH变量,但是一旦出错,很容易造成无法登陆进入图形界面的问题. ...
- jQuery 模态对话框示例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Podfile使用说明
什么是Podfile ? CocoaPods是用ruby实现的,因此Podfile文件的语法就是ruby的语法.podfile是一个说明文件,用以描述管理一个或者多个Xcode project的tar ...
- 好用的wget命令从下载添加环境变量到各参数详解
本文是因为(笔者使用的windows系统)使用过好几次wget后,始终存在各种细节问题,于是下定决定细致的研究一下,并记录下其中细节. 下载与安装 第一步:下载wget,网络地址:http://dow ...
- iOS-绘图
0 CGContextRef context = UIGraphicsGetCurrentCont ext(); 设置上下文 1 CGContextMoveToPoint 开始画线 2 CGCon ...
- spring mvc重定向方法
一.不带参数,直接重定向到另一个地址: 返回String直接跳转,如: @RequestMapping(value = "/filehandle") public String u ...
- 浅谈:javascript的面向对象编程之具体实现
下面的javascript代码都是需要使用jQuery插件来做的.希望大家可以搭建好工作环境 首先我们来做一个练习:在一个删除的超链接中添加一个提示信息,提示是否确认删除. 一般情况下我们都会这么做 ...
- Angularjs -Promise - $http
https://www.peterbe.com/plog/promises-with-$http