jQuery 页面方法

发布于 2024-10-04 09:52:51 字数 2190 浏览 0 评论 0原文

阅读 http:// encosia.com/2008/05/29/using-jquery-to-directly-call-aspnet-ajax-page-methods/ 我决定自己尝试一下。我现在想知道为什么我的ajax没有被调用。非常感谢!

背后的代码

TestAjax.cs

using System;
using System.Collections.Generic;
//using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;

public partial class TestingAjax : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    [WebMethod]
    public static string GetDate()
    {
        return DateTime.Now.ToString();
    }
}

和TestingAjax.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestingAjax.aspx.cs" Inherits="TestingAjax" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <script type="text/javascript" src="jquery-1.3.2.min.js"></script>

    <script  type="text/javascript"  language = "javascript" >
        $(document).ready(function () {
            // Add the page method call as an onclick handler for the div.
            $("#Result").click(function () {
                alert("entre en el click:");
                $.ajax({
                    type: "post",
                    url: "TestingAjax.aspx/GetDate",
                    data: "{}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (msg) {
                        alert("Returning from Ajax");
                        // Replace the div's content with the page method's return.
                        $("#Result").text(msg.d);
                    }
                });
            });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
   <div id="Result">Click here for the time.</div>
    </form>
</body>
</html>

After reading http://encosia.com/2008/05/29/using-jquery-to-directly-call-aspnet-ajax-page-methods/ I dediced to try it myself. I'm now wondering why my ajax is not being called. Many thanks!

code behind

TestingAjax.cs

using System;
using System.Collections.Generic;
//using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;

public partial class TestingAjax : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    [WebMethod]
    public static string GetDate()
    {
        return DateTime.Now.ToString();
    }
}

and TestingAjax.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestingAjax.aspx.cs" Inherits="TestingAjax" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <script type="text/javascript" src="jquery-1.3.2.min.js"></script>

    <script  type="text/javascript"  language = "javascript" >
        $(document).ready(function () {
            // Add the page method call as an onclick handler for the div.
            $("#Result").click(function () {
                alert("entre en el click:");
                $.ajax({
                    type: "post",
                    url: "TestingAjax.aspx/GetDate",
                    data: "{}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (msg) {
                        alert("Returning from Ajax");
                        // Replace the div's content with the page method's return.
                        $("#Result").text(msg.d);
                    }
                });
            });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
   <div id="Result">Click here for the time.</div>
    </form>
</body>
</html>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

长伴 2024-10-11 09:52:52

记得在页面的 ScriptManager 上设置属性 EnablePageMethods=true

remember to set the property EnablePageMethods=true on the ScriptManager of the page

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文