从Jquery-Ajax调用Web方法..错误:内部服务器错误

mcU*_*ser 2 asp.net jquery

我正在学习JQuery,今天我正在制作的是级联下拉(国家州和城市).

我的代码到目前为止

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
    <script type="text/javascript" src="JQuery/jquery-1.6.1.min.js"></script>

    <script type="text/javascript">
        $(document).ready(function() {
            $("#ddlCountry").change(function() {
                var CountryID = $("#ddlCountry option:selected").val();

                $.ajax(
            {
                type: "POST",
                url: "CascadingDropDown.aspx/GetSelectedStates",
                data: "{countryID:'" + CountryID + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(data) {
                    alert(data);

                },
                error: function() { alert(arguments[2]); }
            });

            });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table>
        <tr>
        <td>Country</td>
        <td>
            <asp:DropDownList ID="ddlCountry" runat="server"/>
        </td>
        <td>State</td>
        <td>
            <asp:DropDownList ID="ddlState" runat="server"/>
        </td>
        <td>City</td>
        <td>
            <asp:DropDownList ID="ddlCity" runat="server"/>
        </td>
        </tr>

        <tr>
        <td colspan="2">
            <asp:Button ID="btnSave" runat="server" Text="Save" />

        </td>
        <td>
        <asp:Label ID="lblMessage" runat="server"></asp:Label>
        </td>
        </tr>
        </table>
    </div>
    </form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

网络方法如下

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Services;

public partial class CascadingDropDown : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            PopulateCountry();
        }
    }

    private void PopulateCountry()
    {
        DataSource ds = new DataSource();
        ddlCountry.DataSource = ds.GetCountryList();
        ddlCountry.DataValueField = "CountryCode";
        ddlCountry.DataTextField = "CountryName";
        ddlCountry.DataBind();
    }

    [WebMethod]
    public List<State> GetSelectedStates(string countryID)
    {     
        DataSource ds = new DataSource();
        var stateList = ds.GetStateList();

        var result = stateList.Where(i => i.CountryCode == countryID);
        return result.ToList<State>();
    }
}
Run Code Online (Sandbox Code Playgroud)

但是当我选择来自该国家的任何国家时,我都会收到"内部服务器错误".

谁能帮助我指出我正在犯的错误以及如何克服这个错误?

小智 6

您需要static签名中的关键字.在VB.net中你会使用shared.

例如:

C#: public static string DoSomething()

VB: Public Shared Function DoSomething()