$ .getJSON无法调用Code Behind [WebMethod] asp,net

时间:2013-10-24 16:34:49

标签: jquery asp.net getjson

我是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;
    }

我正在生成页面的页面是&#34; MyForm.aspx&#34;这是我的页面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);
        }

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

2 个答案:

答案 0 :(得分:1)

将方法标记为静态

public static string GetJsonData()

答案 1 :(得分:-1)

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