2013-07-25 37 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 

      }