Make multiple Ajax calls on an ASPX or ASHX page

Source: Internet
Author: User
Tags switch case jquery library

In the development of asp. use the ASHX page to interact with the foreground page. but each Ajax interaction requires a ashx page. The result is a whole bunch of ashx pages in the Project. it is difficult to manage the Project. now we'll find a way to allow multiple Ajax interactions on a ashx page ;

Front page ajaxtest.htm, content is as follows

<! DOCTYPE HTML Public "-//w3c//dtd XHTML 1.0 transitional//en" "HTTP://WWW.W3.ORG/TR/XHTML1/DTD/XHTML1-TRANSITIONAL.DTD ">

<title> This page interacts with the background in different ways </title>
<script type= "text/javascript" src= "js/jquery-1.4.2.min.js" ></script>
<script type= "text/javascript" >

Ajax interaction using the jquery library
$ (document). Ready (function () {

//make an ajax request, command tells the backend which method to invoke
$.get ("handler.ashx", {command: "method1", value: "chentao"},function (data) {
Alert (data);
});

Make an ajax request, command tells the backend to call the Method2 method



$.get ("handler.ashx", {command: "method2", value: "tangyu"},function (data) {

Alert (data);
})

</script>
<body>

</body>

Background create a Handler.ashx page content as follows

<%@ 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)

{

Get the command from the foreground to determine which method to Call.


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 ());
}

}

If there are multiple methods, there will be a lot of judgment in the switch Case. consider a simpler approach. using reflection

<%@ 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 ());
}

}

Using reflection greatly simplifies the Program.

=====================================================

Interacting with Ajax using ASPX pages

Create a new ASPX page webmethod.aspx

Delete the redundant parts of the webmethod.aspx page, leaving only

<%@ page language= "c #" autoeventwireup= "true" codefile= "WebMethod.aspx.cs" inherits= "WebMethod"%>

This statement

WebMethod.aspx.cs content is as follows

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 page and webmethod.aspx page for Ajax interaction test.html page content

<! DOCTYPE HTML Public "-//w3c//dtd XHTML 1.0 transitional//en" "HTTP://WWW.W3.ORG/TR/XHTML1/DTD/XHTML1-TRANSITIONAL.DTD ">

    <title> Interactive </title> using aspx pages;
    <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>
<body>

</body>

Make multiple Ajax calls on an ASPX or ASHX page

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.