不使用webmethod的asp网中的jquery ajax方法

时间:2015-09-16 13:36:10

标签: c# asp.net webforms

Aspx Page

 <script>
 $(document).ready(function () {
    $.ajax({
            type: "POST",
            url: "WebForm1.aspx/GetData",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (response) {
                $("#Content").text(response.d);
            },
            failure: function (response) {
                alert(response.d);
            }
        });
    });
</script>
</head>
<body>
<form id="frm" method="post">
    <div id="Content">
    </div>
</form>
</body>
</html>

背后的代码

public static string GetData()
{
    return "This string is from Code behind";
}

我希望在不使用“WEBMETHOD”的情况下使用ajax获取此函数。即GetData()方法,我想在不使用网络服务的情况下在我的.aspx页面中显示。

1 个答案:

答案 0 :(得分:1)

我不确定我是否收到您的问题,但您可能正在寻找的只是您在页面onload事件中所需的代码:

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.ContentType = "application/json";
        Response.Write("put a valid json string here");
    }