Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling web method from Jquery-Ajax ..Error:Internal Server Error

Tags:

jquery

asp.net

I am learning JQuery and today what I am making is a cascading dropdown (Country State and City).

My code so far is as under

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

The web method is as under

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

But while I am choosing any country from the country drop down, I am getting "Internal Server Error".

Could anyone please help me in point out what mistake I am making and how to overcome this?

like image 540
mcUser Avatar asked Feb 26 '26 05:02

mcUser


1 Answers

You need the keyword static in your signature. In VB.net you would use shared.

For example:

C#: public static string DoSomething()

VB: Public Shared Function DoSomething()

like image 100
Mike Cyrid Avatar answered Feb 27 '26 18:02

Mike Cyrid