Using jQuery's getJSON method with an ASP.NET Web Form

会有一股神秘感。 提交于 2019-11-27 04:24:26

问题


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

The goal is this:

  1. User clicks on a list item
  2. The value is sent to the server
  3. Server responds with related list of stuff, formatted using JSON
  4. Populate secondary box

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.

Thanks,
Kieron

Some code:

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

回答1:


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>



回答2:


I tweaked your code a bit. I added the server side output of the ClientID to the updateRegions method to pass it in. And I changed your getJSON method to pass in the url with a query string parameter (instead of separate data) and the call back function.

jQuery(document).ready(function() {
   jQuery("#<%= AreaListBox.ClientID %>").click(function() {
       updateRegions(<%= AreaListBox.ClientID %>);
   });
});

function updateRegions(areaId) {
    jQuery("h2").html("AreaId:" + areaId);

    jQuery.getJSON("/Locations.aspx/GetRegions?" + areaId, 
        function (data, textStatus) {
            debugger;
        });
}

Let me know if that works!




回答3:


You can also use a GetJSON, but you should alter the WebMethod in that case. You should decorate it with:

[WebMethod(EnableSession = true)]       
[ScriptMethod(UseHttpGet =false, ResponseFormat = ResponseFormat.Json)]

Doing a get might not be what you desire.



来源:https://stackoverflow.com/questions/1176603/using-jquerys-getjson-method-with-an-asp-net-web-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!