JQuery Ajax calls two common methods (not ashx) of background aspx background files _jquery

Source: Internet
Author: User
Tags call back

In ASP.net webform development, there are two common ways to call ASPX pages with jquery Ajax: Let me briefly introduce.

(1) Processing by Aspx.cs static method +webmethod

Simple introduction of the use of the WebMethod method

1. Modifiers are decorated primarily with public static

2. The method preceded by the [WebMethod] attribute indicates that this is the WebMethod method

3. Foreground HTML page (client side) access to use the Post method, and background. cs file Data interaction, otherwise it will return the entire HTML page.

4. When the background page returns data, the foreground HTML page needs to receive the returned JSON string with DATA.D.

5. Access Url:http://abc.com/abc.aspx/ajax method

Aspx.cs Code:

Using System.Web.Services; 
[WebMethod]
public static string SayHello ()
{return
"Hello ajax!";
}

Front-Office jquery Code:

$ (function () { 
$ ("#btn"). Click (function () { 
$.ajax ({ 
type: Post),//To post by 
URL: demo.aspx/ SayHello ",//method on the page and method name
contentType:" Application/json; Charset=utf-8 ", 
dataType:" JSON, " 
success:function (data) { 
alert (DATA.D);//returned data gets content in DATA.D
},
error:function (ERR) { 
alert (err); 
} 
} 
);};

HTML code:

<form id= "Form1" runat= "Server" > <div> <asp:button id= "btn" runat= "
Server" text= "Authenticate users"/ >
</div>
</form>

(2) processing through the general processing procedure ashx;

jquery Code:

$.ajax ({ 
type: "POST", 
URL: "S_cbfbm.ashx", 
data: {ZBM:P_ZDM}, 
beforesend:function () { 
//$ ("# Div_load "). Visible =" true; 
}, 
success:function (msg) { 
//$ ("#div_load"). Visible = false; 
$ ("#ds"). HTML ("<p>" + msg + "</p>"); 
$ ("#CBFBM"). Val (msg); 
} 
);

Ashx.cs Code:

<%@ WebHandler language= "C #" class= "Ajaxhandler"%> 
using System; 
Using System.Web; 
public class Ajaxhandler:ihttphandler {public 
void ProcessRequest (HttpContext context) {context 
. Response.ContentType = "Text/plain"; 
if (context. request["Name"]. ToString () = = "Admin" && context 
. Request["Pass"]. ToString () = "admin") 
{context 
. Response.Write ("Y"); 
} 
else 
{context 
. Response.Write ("N"); 
} 
public bool IsReusable {get 
{return 
false; 
}}}

The above is a small series to introduce the jquery Ajax call back to the background aspx file two common methods (not ashx), I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.