Asp.net
在 ASP.NET Web 窗體中使用 jQuery 的 getJSON 方法
如何使用 jQuery 上的 getJSON 方法呼叫 ASP.NET Web 窗體頁面上的方法?
目標是這樣的:
- 使用者點擊列表項
- 值被發送到伺服器
- 伺服器響應相關的東西列表,使用 JSON 格式化
- 填充輔助框
我不想使用 UpdatePanel,我已經使用 ASP.NET MVC 框架完成了數百次,但無法使用 Web 窗體解決這個問題!
到目前為止,我可以做所有事情,包括呼叫伺服器,只是沒有呼叫正確的方法。
謝謝,
基龍
一些程式碼:
jQuery(document).ready(function() { jQuery("#<%= AreaListBox.ClientID %>").click(function() { updateRegions(jQuery(this).val()); }); }); function updateRegions(areaId) { jQuery.getJSON('/Locations.aspx/GetRegions', { areaId: areaId }, function (data, textStatus) { debugger; }); }
這是一個簡約的範例,希望可以幫助您入門:
<%@ Page Language="C#" %> <%@ Import Namespace="System.Web.Services" %> <script runat="server"> [WebMethod] public static string GetRegions(int areaId) { return "Foo " + areaId; } </script> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>jQuery and page methods</title> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script> </head> <body> <script type="text/javascript"> $(function() { var areaId = 42; $.ajax({ type: "POST", url: "Default.aspx/GetRegions", data: "{areaId:" + areaId + "}", contentType: "application/json; charset=utf-8", dataType: "json", success: function(data) { alert(data.d); } }); }); </script> </body> </html>