在asp.net中调用$ .getJSON

时间:2013-07-25 05:50:21

标签: jquery asp.net ajax asp.net-ajax

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

请帮忙.. 感谢..

1 个答案:

答案 0 :(得分: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

            }