0

我是 jquery 的新手。实际上我无法调用 ProcesAPI.aspx 页面中的 WebMethod。

[WebMethod(EnableSession = true)]
    public string GetJsonData()
    {
        JavaScriptSerializer js = new JavaScriptSerializer();
        var persons = new List<Person>
                              {
                                  new Person{Id = 1, FirstName = "F1", 
                                      LastName = "L1", 
                                      Addresses = new List<Address>
                                                      {
                                                          new Address{Line1 = "LaneA"},
                                                          new Address{Line1 = "LaneB"}
                                                      }},

                                  new Person{Id = 2, FirstName = "F2", 
                                      LastName = "L2", 
                                      Addresses = new List<Address>
                                                      {
                                                          new Address{Line1 = "LaneC"},
                                                          new Address{Line1 = "LaneD"}
                                                      }}};

        string result = js.Serialize(persons);
        return result;
    }

我从中生成页面的页面是“MyForm.aspx”,这是我的页面 frpm,我正在调用我的 processApi GetJsonData 方法。

 $(document).ready(function () {
            $('#btnSubmit').click(function () {
                $.getJSON('ProcessAPI.aspx\GetJsonData', null, function (data) {
                    var div = $('#ajaxDiv');
                    div.html("<br/> " + "Persons received from server: " + "<br/>");
                });
                alert('Hii I ');
                return false;
            });
        });

        function printPerson(div, item) {
            div.append("<br/>" + "FName: " + item.FirstName + ", LName: " + item.LastName);
            $.each(item.Addresses, function (i, addr) {
                printAddress(div, addr);
            });
        }

        function printAddress(div, item) {
            div.append("<br/>" + "   " + "Line1: " + item.Line1);
        }

但我的方法没有被调用...... :(

4

2 回答 2

1

将方法标记为静态

public static string GetJsonData()
于 2013-10-24T16:47:04.867 回答
-1

仅在没有会话的情况下尝试 [WebMethod]

于 2013-10-24T16:44:17.743 回答