2013-10-24 57 views
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); 
     } 

,但我的方法是沒有得到所謂的... :(

回答

1

標記方法靜態

public static string GetJsonData() 
-1

嘗試[的WebMethod]只與沒有會話