PageMethods returning undefined result?
Asked Answered
S

5

7

I have a very simple call to a PageMethod. When I step through my PageMethod in my .cs file, the value looks as expected. However, on the client side I get an undefined result. Any ideas? This should be horribly simple.

Here is my js: (EnablePageMethods="true" in my ASPX page)

function test() {
    alert(PageMethods.MyMethod("Joe Blow"));
}

And here is my C#:

public partial class test : System.Web.UI.Page 
{
    [WebMethod]
    public static string MyMethod(string name)
    {
        return "Hello " + name;
    }
}
Strenta answered 24/8, 2009 at 19:15 Comment(5)
I take it the EnablePageMethods attribute is on a ScriptManager control in your page?Frailty
No, to your second question, I read that earlier...haven't found an answer to this here so far. Thanks.Strenta
Hmmm... this is very strange. I have Page Methods numerous times and have not seen this behaviour before - I set up a demo on my local machine (VS2008 3.5 SP1) and to my surprise PageMethods did not appear to be working correctly for me either. Using firebug (or fiddler) I can see a HTTP Post is made but it calls the onFailure function when it does (interestingly, with no error message). If I call the PageMethod from the console in firebug, again I see the HTTP post and the response this time is the expected one. What version and pack of Visual Studio are you using?Frailty
To clarify, when the PageMethod is called from inside the page, the HTTP Post returns 200 ok and the response is expected, but what happens on the page is that the failure function gets called and the result is "The Server method [xxx] failed". A page refresh then occurs straight after. As I have said, I have used PageMethods numerous times and not seen this behaviour before.Frailty
Basically the same experience I had. I'm running Visual Studio 2008 SP1. The only difference now is that I'm running Windows 7 Enterprise (instead of Vista Ultimate as I was before). Making the call via JSON is working for me so that's what I'm sticking with for now.Strenta
C
1

Check out the following screencast. It explains how to call the PageMethods using JQuery:

http://www.highoncoding.com/Articles/430_Calling_Page_Methods_Using_JQuery_and_Returning_JSON_Result.aspx

Conjunctiva answered 24/8, 2009 at 19:37 Comment(4)
Thank you, this worked. I would like to understand why using jQuery to make a JSON call worked but the "Microsoft way" did not. Thank you for your time!Strenta
I will try out with the Microsoft library and let you know. Thanks!Conjunctiva
Sounds great, I hope you have better luck than I did! :)Strenta
Check out the answer below for calling PageMethods using Microsoft Ajax Library.Conjunctiva
C
7

Here is the answer on how to call PageMethods using MS Ajax. First make sure you have downloaded the latest Ajax library from the MS website.

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

    <input type="button" value="Greeting" onclick="greetings()" />

<script language="javascript" type="text/javascript">

    function greetings() {

       PageMethods.GreetingFromPage(function(response) {

            alert(response);

         });

    }


</script>

   [WebMethod]
        public static string GreetingFromPage()
        {
            return "greeting from page"; 
        }

That is pretty much it!

Conjunctiva answered 25/8, 2009 at 18:15 Comment(2)
Thanks, the syntax here is slightly different from what I was using, I'll give this a shot.Strenta
what if the function has parameters?Periodontics
A
3

You've to pass in a callback function that would be executed on Success / Exception. So in this case, it would be something like this

PageMethods.MyMethod("Joe Blow", onSuccess, onError);

function onError(desc) {
}

function onSuccess(result) {
}

I would check the documentation for the exact usage.

Antiphon answered 24/8, 2009 at 19:53 Comment(0)
C
1

Check out the following screencast. It explains how to call the PageMethods using JQuery:

http://www.highoncoding.com/Articles/430_Calling_Page_Methods_Using_JQuery_and_Returning_JSON_Result.aspx

Conjunctiva answered 24/8, 2009 at 19:37 Comment(4)
Thank you, this worked. I would like to understand why using jQuery to make a JSON call worked but the "Microsoft way" did not. Thank you for your time!Strenta
I will try out with the Microsoft library and let you know. Thanks!Conjunctiva
Sounds great, I hope you have better luck than I did! :)Strenta
Check out the answer below for calling PageMethods using Microsoft Ajax Library.Conjunctiva
D
0

This is a great and concrete article on the subject.

For me, the following code is working.

I have a page that processes an excel file asynchronously; while processing, the function EsperarFinDelCargue() polls a PageMethod called CargueFinalizo() each second to see if processing has ended. When processing finishes, a redirection takes place.

OnCallFinalizoComplete is the callback function for the PageMethod invocation, so there is where you need to use the resulting object.

<script type="text/javascript">       

    function EsperarFinDelCargue()
    {           
        PageMethods.CargueFinalizo(OnCallFinalizoComplete);            
        if($('#<%=this.hidCargueFinalizado.ClientID %>').val() == "SI")
        {
            document.location = "CargarPanelHCP.aspx";
        }
        else
        {
            var t=setTimeout("EsperarFinDelCargue()",1000);                
        }
    }

    function OnCallFinalizoComplete(result,contexto,CargueFinalizo)
    {               
        $('#<%=this.hidCargueFinalizado.ClientID %>').val(result);            
    }
</script>

And here is the PageMethod code in the aspx:

[System.Web.Services.WebMethod]
public static string CargueFinalizo()
{
    //Whatever you need
    return HttpContext.Current.Session["ResultadoCarguePanel"] != null ? "SI" : "NO";
}
Disraeli answered 27/3, 2012 at 20:18 Comment(0)
M
0
Try This it will work fine

     <script type="text/javascript">
       function Generate()
       {              
           var result = PageMethods.GenerateOTP(your parameter, function (response)
           {
               alert(response);
           });
       }
</script>
Murillo answered 20/12, 2013 at 6:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.