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://ylbtech.cnblogs.com/ 本文版權歸作者和部落格園共有,歡迎轉載,但未經作者同意必須保留此段宣告,且在文章頁面明顯位置給出原文連線,否則保留追究法律責任的權利。 |
最終目標 |
“程式碼的國際化標準示例 ylb,tech”,最大程度地規範軟體程式設計開發統一,優質, 高效,易學,為建設軟體強國(中國)而努力。