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!