在 ASP.NET Web 窗体中使用 jQuery 的 getJSON 方法 [英] Using jQuery's getJSON method with an ASP.NET Web Form

查看:17
本文介绍了在 ASP.NET Web 窗体中使用 jQuery 的 getJSON 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How do I go about calling a method on an ASP.NET Web Form page using the getJSON method on jQuery?

目标是这样的:

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

我不想使用 UpdatePanel,我已经使用 ASP.NET MVC 框架完成了数百次,但无法使用 Web 窗体弄清楚!

I don't want to use an UpdatePanel, I've done this hundreds on times using the ASP.NET MVC Framework, but can't figure it out using Web Forms!

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

So far, I can do everything, including calling the server, it just doesn't call the right method.

谢谢,
基隆

一些代码:

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;
        });
}

推荐答案

以下是一个极简示例,希望能帮助您入门:

Here is a minimalistic example which should hopefully get you started:

<%@ 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>

这篇关于在 ASP.NET Web 窗体中使用 jQuery 的 getJSON 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆