2

I was creating a simple web method to access from Java script..But I am not able to

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


        $(document).ready(
        function test() {
            var x = PageMethods.MyMethod();
            alert(x.toString());
        })
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" EnablePartialRendering="true">
        </asp:ScriptManager>
    </div>
    </form>
</body>
</html>

Code behind looks like this

 [WebMethod]
        public static string MyMethod()
        {
            return "Hello";
        }

Variable x is null. Iam not able to figure out what is is that Iam missing ?Any help ? Thanks in Advance

2
  • Where do you invoke function test? Commented Aug 3, 2011 at 19:28
  • @Chris .. its automatically invoked when document is ready Commented Aug 3, 2011 at 19:32

1 Answer 1

3

You will need to define a callback function which is invoked upon reciept of the response from the web method:

    $(document).ready(
    function test() {
        PageMethods.MyMethod(myMethodCallBackSuccess, myMethodCallBackFailed);
    })

    function myMethodCallBackSuccess(response) {
        alert(response);
    }

    function myMethodCallBackFailed(error) {
        alert(error.get_message());
    }

You may also pass arguments to the method however these must always come before the success and failure callbacks.

Note: you do not need to include the failed call back but it is available if required.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.