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 页面完全相同...我无法弄清楚到底是什么问题..

请帮忙..谢谢..

4

1 回答 1

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

            }
于 2013-08-12T13:18:26.063 回答