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

从Jquery Ajax调用web方法..错误:内部服务器错误,jquery,asp.net,Jquery,Asp.net,我正在学习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

我正在学习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>
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>();
    }
}

无标题页
$(文档).ready(函数(){
$(“#ddlCountry”).change(function(){
var CountryID=$(“#ddlCountry选项:选中”).val();
$.ajax(
{
类型:“POST”,
url:“CascadingDropDown.aspx/GetSelectedStates”,
数据:“{countryID:'”+countryID+“}”,
contentType:“应用程序/json;字符集=utf-8”,
数据类型:“json”,
成功:功能(数据){
警报(数据);
},
错误:函数(){alert(参数[2]);}
});
});
});
国家
陈述
城市
web方法如下所示

<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>
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>();
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web.Services;
公共部分类CascadingDropDown:System.Web.UI.Page
{
受保护的无效页面加载(对象发送方、事件参数e)
{
如果(!IsPostBack)
{
人口国家();
}
}
私人无效人口国家()
{
数据源ds=新数据源();
ddlCountry.DataSource=ds.GetCountryList();
ddlCountry.DataValueField=“CountryCode”;
ddlCountry.DataTextField=“CountryName”;
ddlCountry.DataBind();
}
[网络方法]
公共列表GetSelectedState(字符串countryID)
{     
数据源ds=新数据源();
var stateList=ds.GetStateList();
var result=stateList.Where(i=>i.CountryCode==countryID);
返回result.ToList();
}
}
但是,当我从国家/地区下拉列表中选择任何国家/地区时,我得到了“内部服务器错误”。


有人能帮我指出我犯了什么错误以及如何克服这个错误吗?

内部服务器错误
表示您的服务器端代码中有一个bug。只需启动Visual Studio调试器并在
GetSelectedStates
方法中设置断点。然后,你应该找出引起麻烦的线路

编辑:

如果没有到达方法中的断点,则可能是传递给方法的参数有问题。尝试在countryID的周围添加引号:

data: "{'countryID':'" + CountryID + "'}"
编辑2:

如果它仍然不能解决您的问题,那么最好的方法是使用它来了解发生了什么


在它的帮助下,您可以跟踪发送到服务器的每个请求以及参数。此外,您还可以将特定请求拖放到Fiddler的请求生成器,只需单击一个按钮即可重新执行请求,这是测试web方法的一种非常方便的方法。

尝试用以下内容装饰您的方法:

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
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>();
}
[WebMethod]
[ScriptMethod(ResponseFormat=ResponseFormat.Json)]
公共列表GetSelectedState(字符串countryID)
{     
数据源ds=新数据源();
var stateList=ds.GetStateList();
var result=stateList.Where(i=>i.CountryCode==countryID);
返回result.ToList();
}
它来自System.Web.Script.Services命名空间

编辑:

您能否尝试调整AJAX调用:

<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 + "'}",
            dataType: "json",
            success: function(data) {
                alert(data);

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

        });
    });
</script>

$(文档).ready(函数(){
$(“#ddlCountry”).change(function(){
var CountryID=$(“#ddlCountry选项:选中”).val();
$.ajax(
{
类型:“POST”,
url:“CascadingDropDown.aspx/GetSelectedStates”,
数据:“{countryID:'”+countryID+“}”,
数据类型:“json”,
成功:功能(数据){
警报(数据);
},
错误:函数(){alert(参数[2]);}
});
});
});

与中一样,去掉JSON响应预期,看看它是否可以作为文本使用?

您确定不想使用PageMethod吗?这些需要标记为静态才能正常工作

 [WebMethod]
public static 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>();
}
[WebMethod]
公共静态列表GetSelectedStates(字符串countryID)
{     
数据源ds=新数据源();
var stateList=ds.GetStateList();
var result=stateList.Where(i=>i.CountryCode==countryID);
返回result.ToList();
}

据我所知,除了使用Web服务之外,这是将WebMethod放入ASPX页面的唯一方法。

签名中需要关键字
static
。在VB.net中,您将使用
shared

例如:

C:
公共静态字符串DoSomething()


VB:
公共共享函数DoSomething()
我还没有看到这个问题的答案,所以我要发布我的答案。您需要将
ArgementName
设置为等于某个值。至少这就是我解决问题的原因

看看我的javascript

var teamID = 123;
        var playerID = 456;
        var active = true;
        var webMethod = 'Service1.asmx/HelloWorld'
        var parameters = "{'active':'" + active +
                 "','team_id':'" + teamID +
                 "','player_id':'" + playerID + "'}"

        $.ajax({
            type: "POST",
            url: webMethod,
            data: '{ArgumentName:"'+ parameters +'"}', 
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(msg) {
                document.write('all good' + var_dump(msg));
            },
            error: function(e) {
                document.write('err' + var_dump(e));
            }
        });
        function var_dump(obj) {
            var obj_members = "";
            var sep = "";
            for (var key in obj) {
                obj_members += sep + key + ":" + obj[key];
                sep = ", ";
            }
            return ("[" + obj_members + "]");
        }

调试时会发生什么?这不太可能是AJAX/JSON问题,但可能是GetStateList()方法中的问题?它甚至还没有达到这一点。。。我已经在那个方法中加入了断点,它甚至还没有达到那个点。。。我在该方法中添加了断点(编辑答案,查看!)是不是因为我没有使用.asmx,在一个简单的aspx页面中,我添加了一个使用System.Web.Services的参考;并用[WebMethod]属性修饰方法?响应文本显示为“未知的web方法GetSelectedState.
参数名称:methodName”从用[WebMethod]属性修饰的ajax调用方法应该没有问题。这只是构造正确的ajax请求的问题,但这也碰巧没有什么好运气!是CascadingDropDown。