无法加载WebMethod

时间:2017-08-03 05:33:52

标签: c# jquery ajax

我的下拉更改功能很好用。但WebMethod无法加载

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            $("[id*=drpSub]").change(function () {
                var drpSub = $("[id*=drpSub]").val();
                var Gs_Id = $("[id*=ddlClass]").val();
                alert(drpSub);
                $.ajax({
                    type: 'GET',
                    url: 'assignments1.aspx/GetAssignmentType',
                    data: {},
                    dataType: 'json',
                    success: function (r) {
                        //var ddlCustomers = $("[id*=drpType]");
                        //ddlCustomers.empty().append('<option selected="selected" value="0">Please select</option>');
                        //$.each(r.d, function () {
                        //    ddlCustomers.append($("<option></option>").val(this['Value']).html(this['Text']));
                        //});
                    }
                });
            });
        });

代码背后

[WebMethod]
public static List<ListItem> GetAssignmentType()
    {
       string query = "SELECT OP_AssignmentType.AT_Id, OP_AssignmentType.AT_Type + ' (' + CONVERT(varchar(10), COUNT(8)) + ')' AS AT_Type FROM OP_Assignments INNER JOIN OP_AssignmentType ON OP_Assignments.OP_AS_TypeId = OP_AssignmentType.AT_Id WHERE        (OP_Assignments.OP_AS_SubjId = '" + 10 + "') AND (OP_Assignments.OP_GS_IdNo = 37) And OP_AS_LastDate>='" + DateTime.Now + "' GROUP BY OP_AssignmentType.AT_Id, OP_AssignmentType.AT_Type";
        string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand(query))
            {
                List<ListItem> customers = new List<ListItem>();
                cmd.CommandType = CommandType.Text;
                cmd.Connection = con;
                con.Open();
                using (SqlDataReader sdr = cmd.ExecuteReader())
                {
                    while (sdr.Read())
                    {
                        customers.Add(new ListItem
                        {
                            Value = sdr["Sub_Id"].ToString(),
                            Text = sdr["Subject_Name"].ToString()
                        });
                    }
                }
                con.Close();
                return customers;
            }
        }
    }

我知道Web方法中的代码可能有误。我的实际问题是,在Web方法代码中放置断点时页面没有加载。我的控制台没有错误。他们的jQuery中有任何错误。请帮忙

1 个答案:

答案 0 :(得分:0)

你必须在你的ajax中添加contentType: "application/json; charset=utf-8",所以,你的ajax就像

          $.ajax({
                type: 'GET',
                url: 'Default.aspx/GetAssignmentType',
                data: {},
                contentType: "application/json; charset=utf-8",
                dataType: 'json',
                success: function (r) {
                    //var ddlCustomers = $("[id*=drpType]");
                    //ddlCustomers.empty().append('<option selected="selected" value="0">Please select</option>');
                    //$.each(r.d, function () {
                    //    ddlCustomers.append($("<option></option>").val(this['Value']).html(this['Text']));
                    //});
                }
            });