开发者

Unable to access a webmethod from Javascript

开发者 https://www.devze.com 2023-03-24 20:40 出处:网络
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\">

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>
    <s开发者_高级运维cript 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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消