【问题标题】:Ajax call to webmethod gives ERROR 500对 webmethod 的 Ajax 调用给出 ERROR 500
【发布时间】:2017-11-23 21:43:08
【问题描述】:

我是一个初学者,当另一个下拉列表选择的索引发生变化时,我想使用 ajax 将一些数据从数据库加载到下拉列表 但我得到的只是 500 错误 我的 jquery ajax 代码

 function ddlGroups() {
        var s = $("#Content_ddlGroups").find("option:selected").prop("value");

        $.ajax({
            method: "GET",
            contentType: "application/json; charset=utf-8",
            //url is the path of our web method (Page name/function name)
            url: "../panels/admin/AddProject.aspx/getSubgroups",
            data: { Id: s },
            dataType: "json",
            //called on jquery ajax call success
            success: function (result) {
                $('#Content_SubGroups').empty();

                $.each(result.d, function (key, value) {
                    $("#Content_ddlGroups").append($("<option></option>").val(value.GroupID).html(value.Title));

                });

            },
            //called on jquery ajax call failure
            error: function ajaxError(result) {
                alert(result.status + ' : ' + result.statusText);
            }
        });
    };

还有我的 c# 代码

 [WebMethod]
    [ScriptMethod(UseHttpGet = true)]
    public static List<Group> getSubgroups(string Id)
    {
        DataTable dt = new DataTable();
        List<Group> objDept = new List<Group>();
        GroupsRepository jg = new GroupsRepository();
        //Page page = (Page)HttpContext.Current.Handler;
        //DropDownList DDLGroups = (DropDownList)page.FindControl("DDLGroups");

        dt = jg.LoadSubGroup(Id.ToInt());
        if (dt.Rows.Count > 0)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                objDept.Add(new Group
                {
                    GroupID = Convert.ToInt32(dt.Rows[i][0]),
                    Title = dt.Rows[i][1].ToString(),
                });
            }
        }
        return objDept;
    }

有什么问题??

【问题讨论】:

  • 500 表示抛出异常。您可以调试您的代码,或者至少添加一个try-catch
  • 要添加到 Camilo 的评论,错误发生在“getSubgroups”代码中,因此您需要添加 try catch 以查看实际错误。
  • @Sparrow 我怎样才能看到错误?我在代码中添加了 try catch 但我不知道如何显示错误
  • 嗯...您可以在 catch 块中添加一些语句来记录(写入)异常的消息和/或某处的堆栈跟踪(通常应用程序有这些日志的文件或表)。您还可以在服务中设置断点并手动调试。 Yo 甚至可以使用 Debug 类(在 System.Diagnostics 命名空间中)来查看哪一行抛出异常以及异常是什么
  • 由于您是初学者,我认为最简单的选择是使用诊断

标签: javascript c# jquery asp.net ajax


【解决方案1】:

jQuery 不是这里的问题。 500 错误是服务器抛出的,因此您应该查看您的 c# 代码的日志以找出有关它的详细信息并能够缩小原因。

【讨论】:

    【解决方案2】:

    在您的 AJAX 调用中实际上是 500 错误导致因为您已通过 Id:s 而不是 Id 有一些数字。前任。编号:5。

    如果您在服务器端方法中将字符串作为 id 传递,我会看到您的代码 您正在尝试将该字符串转换为 int。这实际上会导致 500 错误。

    [WebMethod]
        [ScriptMethod(UseHttpGet = true)]
        public static List<Group> getSubgroups(string Id)
        {
            DataTable dt = new DataTable();
            List<Group> objDept = new List<Group>();
            GroupsRepository jg = new GroupsRepository();
            //Page page = (Page)HttpContext.Current.Handler;
            //DropDownList DDLGroups = (DropDownList)page.FindControl("DDLGroups");
    
            dt = jg.LoadSubGroup(Id.ToInt()); // Here You have convert string to Int that's why you got 500 Error.
    
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    objDept.Add(new Group
                    {
                        GroupID = Convert.ToInt32(dt.Rows[i][0]),
                        Title = dt.Rows[i][1].ToString(),
                    });
                }
            }
            return objDept;
        }
    

    【讨论】:

    • 我收到此错误 ::Invalid web service call, missing value for parameter: 'Id'
    • 嗨.Javad。使用 Json.stringify。例如。数据:JSON.stringify({Id:5})。这有帮助。另一种方式是 data:"{'Id':'" +"s"+ "'}"
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 2011-12-07
    • 2017-04-16
    • 2016-03-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多