PageMethod无法调用C#函数

时间:2020-11-27 21:35:52

while attempting to learn PageMethods, I've come across a peculiar problem.

在尝试学习PageMethods时,我遇到了一个特殊的问题。

When running the program, the program enters the error function. Chrome developer tools shows the following error:

运行程序时,程序进入错误功能。 Chrome开发者工具显示以下错误:

    GET http://localhost:62316/Advertising/WebForm1.aspx/GetCurrentDate  ScriptResource.axd:4844

Here is my asp code:

这是我的asp代码:

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

        function call() {
            PageMethods.GetCurrentDate(success, fail);
            return false;
        }


        function success(msg) {
            alert("Success "+msg);
        }

        function fail(xhr, status) {
            alert("Fail "+xhr.responseText+" "+status);
        }
    </script> 
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="Manager" runat="server" EnablePageMethods="true"></asp:ScriptManager>
        <asp:Button ID="test" Text="Get Text" runat="server" OnClientClick="javascript: return call();" />

    </form>
</body>
</html>

Here is the C# code:

这是C#代码:

     [WebMethod]
    [ScriptMethod(UseHttpGet = true)]
    public static string GetCurrentDate()
    {

        return "foo";
    }

The objects that are shown in the error box are undefined and null respectively. Furthermore, with a breakpoint in the function, it has been concluded that the function is never stepped into. What am I doing wrong?

错误框中显示的对象分别是undefined和null。此外,通过函数中的断点,可以得出结论,函数永远不会进入。我究竟做错了什么?

1 个解决方案

#1


1  

Change the below line

更改以下行

<asp:Button ID="test" Text="Get Text" runat="server" OnClientClick="javascript: return call();" />

as

<asp:Button ID="test" Text="Get Text" runat="server" OnClientClick="call(); return false;" />

It should work

它应该工作

#1


1  

Change the below line

更改以下行

<asp:Button ID="test" Text="Get Text" runat="server" OnClientClick="javascript: return call();" />

as

<asp:Button ID="test" Text="Get Text" runat="server" OnClientClick="call(); return false;" />

It should work

它应该工作