The aspx or ashx pages of ajax are used multiple times.

Source: Internet
Author: User
Tags html page httpcontext min reflection switch case xmlns jquery library

The content of ajaxtest.htm on the front page is as follows:

The code is as follows: Copy code

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" http://www.111cn.net>

<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<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">

// Use the jquery library for ajax interaction
$ (Document). ready (function (){

// Execute an ajax request. The command tells the backend method to call.
$. Get ("Handler. ashx", {command: "method1", value: "chentao"}, function (data ){
Alert (data );
});

// Make an ajax request, and the command tells the background to call the method2 method


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

Alert (data );
})
     
</Script>
</Head>
<Body>

</Body>
</Html>

Set up a Handler. ashx page in the background as follows:

The code is as follows: Copy code

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

// Obtain the command sent from the foreground and determine the 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 many judgments in the switch case. Consider using a simpler method. Use reflection.

The code is as follows: Copy code

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

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

Use the aspx page to interact with ajax

Create an aspx page WebMethod. aspx

Delete the remaining parts of the WebMethod. aspx page and retain only

   

The code is as follows: Copy code
<% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "WebMethod. aspx. cs" Inherits = "WebMethod" %>

This statement

The content of WebMethod. aspx. cs is as follows:

The code is as follows: Copy code

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 '}";
        }
    }
}

Perform ajax interaction between the test.html page and the WebMethod. aspx page. Content of the test.html page

<! 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> use the aspx page for interaction </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>

Related Article

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.