使用jQuery的getJSON方法和ASP.NET Web窗体

Kie*_*ron 11 asp.net jquery webforms getjson

如何使用jQuery上的getJSON方法在ASP.NET Web窗体页面上调用方法?

目标是这样的:

  1. 用户单击列表项
  2. 该值将发送到服务器
  3. 服务器使用相关的东西列表进行响应,使用JSON格式化
  4. 填充辅助框

我不想使用UpdatePanel,我已经使用ASP.NET MVC框架完成了数百次,但无法使用Web窗体来解决这个问题!

到目前为止,我可以做任何事情,包括调用服务器,它只是没有调用正确的方法.

谢谢,
Kieron

一些代码:

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;
        });
}
Run Code Online (Sandbox Code Playgroud)

Ata*_*hev 26

这是一个简约的例子,希望能让你开始:

<%@ 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>
Run Code Online (Sandbox Code Playgroud)