2013-07-25 77 views
0

我曾在我的aspx.cs創建了一個簡單的WebMethod文件,如下圖所示:

[WebMethod] 
    public static Person GetProfile() 
    { 
     return new Person(); 
    } 

Person類是如下:

public class Person 
{ 
    public string Name { get; set; } 
    public Experience[] Exp { get; set; } 

    public Person() 
    { 
     Name = "Animesh Das"; 
     Exp = new Experience[5]; 
     for (int i = 0; i < 5; i++) 
     { 
      Exp[i] = new Experience(); 
      Exp[i].Company = "IBM"; 
      Exp[i].Designation = "Software Developer"; 
     } 
    } 
} 

public class Experience 
{ 
    public string Designation { get; set; } 
    public string Company { get; set; } 
} 

現在我我試圖使用$ .getJSON方法我.aspx頁面中進行Ajax調用如下:

<script> 
    $.get("default.aspx/GetProfile", function (data) { 
     alert(data); 
    }); 

</script> 

但數據對象包含以下數據:

<!DOCTYPE html> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head><title> 

</title> 
<script src="jquery-1.10.2.min.js"></script> 
</head> 
<body> 
    <form method="post" action="GetProfile" id="form1"> 
    <div class="aspNetHidden"> 
    <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE"  value="/wEPDwULLTE2MTY2ODcyMjlkZE8EUCgavLhMrbR5O0gCYV5HOYEgsOzi11GSvHypFwDT" /> 
</div> 

    <div> 
     <div id="profile"></div> 
     <div id="data_div"></div> 
     <div id="example"></div> 
     <script> 
      $.get("default.aspx/GetProfile", function (data) { 
       console.log(data); 
       //$("#profile").html(data); 
      }); 

     </script> 
    </div> 
    </form> 
</body> 
</html> 

什麼是我的aspx頁面完全一樣...... 我無法弄清楚到底是什麼問題..

請幫助.. 感謝..

+0

嘗試使用Ajax調用獲取Webmethod中的響應。 –

回答

0

使用代碼如下

function countMails() { 
       $.ajax({ 
        type: "POST", 
        url: "FrmCSDashBoardNew.aspx/CountMail", 
        data: '', 
        contentType: "application/json; charset=utf-8", 
        dataType: "json", 
        success: OnSuccess, 
        failure: function (response) { 

        } 
       }); 
      } 
      function OnSuccess(response) { 
       //parse response string 

      }