-4

我正在尝试通过 Ajax 将数据发送到我的数据库,在我的 C# 代码中使用 JQuery Ajax 函数和 WebMethod。但它不起作用。有任何想法吗?

$(function() {
    $('#Add').click(function() {
        var $dialog = $('<div id="MyDialog"></div').appendTo('body')
            .load("../Pop-up.aspx #pop-up", createDatePicker)
            .dialog({
                position: 'center',
                width: 550,
                buttons: {
                    "OK": function() {
                        var jname = $('#name').val();
                        var jbirthday = $('#datepicker').val();
                        var jgender = $('input:radio[name=Gender]:checked').val();
                        var jcourseId = $('#DropDownList1').val();
                        var myjson = {
                            name: jname,
                            birthday: jbirthday,
                            gender: jgender,
                            courseId: jcourseId
                        };

                        $.ajax({
                            type: "post",
                            contentType: "application/json;charset=utf-8",
                            dataType: "json",
                            url: "Manage.aspx/AddStudent",
                            data: $.toJSON(myjson)
                        });
                    },
                    "Cancel": function() {
                        $(this).dialog('close');
                    }
                }
            });
        return false;
    });

});

这是我的网络方法

      [WebMethod]
      public static void AddStudent(string birthday,string name,string gender,string courseId)
      {
        var repository = new StudentRepository();
        var student = new Student
                            {
                                Birthday = Convert.ToDateTime(birthday),
                                Name = name,
                                Gender = gender,
                                Course_ID = Convert.ToInt32(courseId)
                            };
        repository.InsertStudent(student);
            repository.Save();
      }
4

2 回答 2

0

try this:

data: JSON.stringify(myjson)

instead of:

data: $.toJSON(myjson)
于 2013-10-22T14:35:45.440 回答
0

Try changing the order of the properties on your JSON object so they match the parameters in your Webmethod.

    var myjson = {
       birthday: jbirthday, //now the properties match the parameters
       name: jname,
       gender: jgender,
       courseId: jcourseId
    };

    public static void AddStudent(string birthday, string name, string gender, 
        string courseId)
    { ... }

I think you're seeing the 500 error because ASP.NET can't find a method signature that matches your deserialized JSON object, even though all the parameters are of type string.

Update

I've had a chance to test out what I originally suggested, and the ordering of properties didn't matter.

Not sure if any of this helps but, I saw 500 errors when:

  • The properties on my JSON object didn't match all the parameters in my WebMethod

    var myjson = {b: 'test1', c: 'test' };
    
    [System.Web.Services.WebMethod]
    public static void TestMethod(string a, string b)
    { 
        //does nothing.
    }
    
  • One of the parameters I was passing in was undefined.

    var myjson = {a: undefined, b: 'test' };
    
    [System.Web.Services.WebMethod]
    public static void TestMethod(string a, string b)
    { 
        //does nothing.
    }
    
于 2012-05-08T03:38:04.857 回答