1,博文简介 |
a)$.post(url,data,callback,type)的应用。
b)$.ajax({type:,url:,data:,success:,dataType:})的应用。
结合一本处理程序处理。
2,a)$.post()例子 |
~/Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!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 runat="server"> <title></title> <script src="script/jquery-1.4.1.js" type="text/javascript"></script> <script type="text/javascript"> var login = function () { var data = { "username": $("#username").val(), "pwd": $("#pwd").val() } $.post("Login.ashx", data, function (message) { if (message.success) { alert(message.msg); } else { alert(message.msg); } }, "json"); } </script> </head> <body> <h3>Login</h3> Username:<input id="username" name="username" type="text" /><br /> Userpass:<input id="pwd" name="pwd" type="password" /><br /> <button type="button" onclick="login()">Submit</button> </body> </html>
3,b)$.ajax()例子 |
~/Default2.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %> <!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 runat="server"> <title></title> <script src="script/jquery-1.4.1.js" type="text/javascript"></script> <script type="text/javascript"> var login = function () { var data = { "username": $.trim($("#username").val()), "pwd": $.trim($("#pwd").val()) } $.ajax({ type: "GET", url: "Login.ashx", data: data, success: function (message) { if (message.success) { //alert(message.msg); location.href = "http://www.baidu.com"; } else { alert(message.msg); } }, dataType: "json" }); $.ajax({}); } </script> </head> <body> <h3>Login</h3> Username:<input id="username" name="username" type="text" /><br /> Userpass:<input id="pwd" name="pwd" type="password" /><br /> <button type="button" onclick="login()">Submit</button> </body> </html>
4,c)处理页面(一般处理程序) |
~/Login.ashx
<%@ WebHandler Language="C#" Class="Login" %> using System; using System.Web; using System.Web.Script.Serialization; //引用,脚本序列化 public class Login : IHttpHandler { public void ProcessRequest (HttpContext context) { context.Response.ContentType = "text/plain"; message msg = null; string username = context.Request["username"]; string pwd = context.Request["pwd"]; if (username == "rain" && pwd == "m123") { msg = new message(true, "S"); } else { msg = new message(false, "F"); } //创建序列化对象 JavaScriptSerializer jss = new JavaScriptSerializer(); context.Response.Write(jss.Serialize(msg)); //返回序列化对象 } public bool IsReusable { get { return false; } } /// <summary> /// 返回序列化对象 /// </summary> class message { public message(bool success, string msg) { this.success = success; this.msg = msg; } public bool success; public string msg; } }
本文转自ylbtech博客园博客,原文链接:http://www.cnblogs.com/ylbtech/archive/2012/10/10/2718044.html,如需转载请自行联系原作者