Question

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

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

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

Here is my asp code:

    <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:

     [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?

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top