0

从下面的 javascript 代码中,我尝试调用服务器端方法,但没有调用服务器端方法。我正在使用 jquery,ajax

<script type="text/javascript" src="JquryLib.js"></script>
<script type="text/javascript" language="javascript">
    function fnPopulateCities() {
        debugger;
        var State = $("#ddlState").val();
        GetCities(State);
        return false;
    }
    function GetCities(StateId) {
        debugger;
        var v1 = 'StateId: ' + StateId;
        $.ajax(
     {
         type: "POST",
         url: 'DropDownList_Cascade.aspx/PopulateCities',
         data: '{' + v1 + '}',
         contentType: "application/json; charset=utf-8",
         dataType: "json",
         success: function (result) {
             if (result.status === "OK") {
                 alert('Success!!');
             }
             else {
                 fnDisplayCities(result);
             }
         },
         error: function (req, status, error) {
             alert("Sorry! Not able to retrieve cities");
         }
     });
 }
</script>

这是我需要调用的服务器端方法。

private static ArrayList PopulateCities(int StateId)
{
    //this code returns Cities ArrayList from database.
}

它给了我以下错误:500(内部服务器错误)

我无法弄清楚出了什么问题。请帮忙!

堆栈跟踪:

[ArgumentException:未知的 Web 方法 PopulateCities.Parameter 名称:methodName]

4

2 回答 2

0

使用这个脚本:

function fnPopulateCities() {
            debugger;
            var State = $("#ddlState").val();
            GetCities(State);
            return false;
        }
        function GetCities(StateId) {
            debugger;

            var data = {
                'StateId': StateId
            };

            $.ajax({
                type: "POST",
                url: 'DropDownList_Cascade.aspx/PopulateCities',
                data: JSON.stringify(data), // using from JSON.stringify is much better than to try stringify data manually
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (result) {
                    if (result.status === "OK") {
                        alert('Success!!');
                    }
                    else {
                        fnDisplayCities(result);
                    }
                },
                error: function (req, status, error) {
                    alert("Sorry! Not able to retrieve cities");
                }
            });
        }

以及后面的代码代码:

    [System.Web.Services.WebMethod]
    public static ArrayList PopulateCities(int StateId)
    {
        //this code returns Cities ArrayList from database.
    }
于 2013-10-26T11:17:01.313 回答
0

使用这个脚本

function GetCities(StateId) {
        debugger;
        var v1 = "{'StateId': '" + StateId+"'}";
        $.ajax({
         type: "POST",
         url: 'DropDownList_Cascade.aspx/PopulateCities',
         data: v1,
         contentType: "application/json; charset=utf-8",
         dataType: "json",
         success: function (result) {
             if (result.status === "OK") {
                 alert('Success!!');
             }
             else {
                 fnDisplayCities(result);
             }
         },
         error: function (req, status, error) {
             alert("Sorry! Not able to retrieve cities");
         }
     });
 }

并修改后面的代码

[System.Web.Services.WebMethod]
public static ArrayList PopulateCities(int StateId)
{
    //this code returns Cities ArrayList from database.
}
于 2013-10-25T23:48:27.563 回答