利用JQuery的$.ajax()可以很方便的呼叫asp.net的後台方法。
先來個簡單的實例熱身吧。
1、無參數的方法呼叫
C#後台程式碼:
using System.Web.Services; [WebMethod] public static string sayHi() { return "Hi,Welcome to China!"; }
注意:1.方法一定要靜態方法,而且要有[WebMethod]的宣告.
html程式碼:
<div> <asp:Button ID="btnClick" runat="server" Text="click me" /> <br /> <span id="msg"></span> </div>
運行結果:
透過firebug能很清楚地看到json回傳的資料格式,所以在取資料的時候要data.d。
2、帶參數的方法呼叫
C#後台代碼:
<script type="text/javascript"> $(document).ready( function() { $("#btnClick").bind("click", function() { $.ajax({ type: "post", url: "ajaxHandler.aspx/sayHi", contentType: "application/json; charset=utf-8", dataType: "json", success: function(data) { $("#msg").css("color", "#0000FF").html(data.d); }, error: function(err) { $("#msg").css("color", "#FF0000").html("access faield:" + err); } }); return false; }); }); </script>
html代碼:
[WebMethod] public static string sayHi(string address, string name) { return "Hi," + address + " " + name; }
jQuery代碼:
<div> <asp:Button ID="btnClick" runat="server" Text="click me" /> address:<asp:TextBox ID="txtAddress" runat="server"></asp:TextBox> family name:<asp:TextBox ID="txtName" runat="server"></asp:TextBox> <br /> <span id="msg"></span> </div>
C#後台程式碼:
<script type="text/javascript"> $(document).ready( function() { $("#btnClick").bind("click", function() { var add = $("#txtAddress").val(); var txtname = $("#txtName").val(); $.ajax({ type: "post", url: "ajaxHandler.aspx/sayHi", data: "{'address':'" + add + "','name':'" + txtname + "'}", contentType: "application/json; charset=utf-8", dataType: "json", success: function(data) { $("#msg").css("color", "#0000FF").html(data.d); }, error: function(err) { $("#msg").css("color", "#FF0000").html("access faield:" + err); } }); return false; }); }); </script>
[WebMethod] public static List<string> sayHi(string address, string name) { List<string> list = new List<string>(); for (int i = 0; i < 10; i++) { list.Add("Hi:" + i.ToString()); } list.Add("Hi:" + address + " " + name); return list; }
jQuery程式碼:
<div> <asp:Button ID="btnClick" runat="server" Text="click me" /> address:<asp:TextBox ID="txtAddress" runat="server"></asp:TextBox> family name:<asp:TextBox ID="txtName" runat="server"></asp:TextBox> <br /> <ul id="msg"> </ul> </div>
.
<script type="text/javascript"> $(document).ready( function() { $("#btnClick").bind("click", function() { var add = $("#txtAddress").val(); var txtname = $("#txtName").val(); $.ajax({ type: "post", url: "ajaxHandler.aspx/sayHi", data: "{'address':'" + add + "','name':'" + txtname + "'}", contentType: "application/json; charset=utf-8", dataType: "json", success: function(data) { $("#msg").html(""); $(data.d).each(function() { $("#msg").append("<li>" + this + "</li>"); }); $("#msg").css("color", "#0000FF"); }, error: function(err) { $("#msg").css("color", "#FF0000").html("access faield:" + err); } }); return false; }); }); </script>
[WebMethod] public static SortedList<string, string> sayHi(string address, string name) { SortedList<string, string> sl = new SortedList<string, string>(); for (int i = 0; i < 10; i++) { sl.Add(i.ToString() + "_key", i.ToString() + "_value"); } sl.Add("_key", "_value " + address + " " + name); return sl; }
<div> <asp:Button ID="btnClick" runat="server" Text="click me" /> address:<asp:TextBox ID="txtAddress" runat="server"></asp:TextBox> family name:<asp:TextBox ID="txtName" runat="server"></asp:TextBox> <br /> <ul id="msg"> </ul> </div>
jQuery程式碼:
<script type="text/javascript"> $(document).ready( function() { $("#btnClick").bind("click", function() { var add = $("#txtAddress").val(); var txtname = $("#txtName").val(); $.ajax({ type: "post", url: "ajaxHandler.aspx/sayHi", data: "{'address':'" + add + "','name':'" + txtname + "'}", contentType: "application/json; charset=utf-8", dataType: "json", success: function(data) { $("#msg").html(""); //这里只取部分键、值显示 $("#msg").append("<li>" + data.d["0_key"] + "</li>"); $("#msg").append("<li>" + data.d["1_key"] + "</li>"); $("#msg").append("<li>" + data.d["2_key"] + "</li>"); $("#msg").append("<li>" + data.d["_key"] + "</li>"); $("#msg").css("color", "#0000FF"); }, error: function(err) { $("#msg").css("color", "#FF0000").html("access faield:" + err); } }); return false; }); }); </script>