PageMethods is not defined error

I have the following code behind method and I want to call it using JScript

VB Code

   <WebMethod>
    Public Shared Function SayHello() As String
        Return ("Hello JScript")
    End Function

ASPX

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script  type="text/javascript">
        function GetMessage() {
            var i = PageMethods.SayHello();
            document.write(i);
        }
        setInterval(GetMessage, 500);
    </script>
</head>
<body>
</body>
</html>

Only I get : Uncaught ReferenceError: PageMethods is not defined

I'm trying to solve this issue but there is no way, need help, please.

Upvotes: 0

Views: 6793

Answers (1)

anmarti
anmarti

Reputation: 5143

You mised the Microsoft Ajax Extensions in your markup.

<asp:ScriptManager ID="ScriptManager1" 
EnablePageMethods="true" 
EnablePartialRendering="true" runat="server" />

<html xmlns="http://www.w3.org/1999/xhtml">
   <head runat="server">
     <title></title>
       <script  type="text/javascript">
          function GetMessage() {
             PageMethods.SayHello(callBack);
           }

           function callBack(result, userContext, methodName){
                 alert(result);
           }
           setInterval(GetMessage, 500);
        </script>
      </head>
     <body>
     </body>
</html>

Despite this is a valid method, I prefer calling page methods using jQuery(MS ajax extensions are not necessary):

You need the jQuery library:

<script   src="http://code.jquery.com/jquery-3.1.1.min.js"   integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8="   crossorigin="anonymous"></script>


function GetMessage() {
 $.ajax({
    type: "POST",
    url: "PageMethods.aspx/SayHello",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(response) {
        alert(response.d);
    },
    failure: function(response) {
        alert("Error");
    }
 });
}

Upvotes: 1

Related Questions