WebMethod / PageMethod problem...

Last post 05-09-2008 2:09 AM by bubblesnout. 8 replies.

Sort Posts:

  • WebMethod / PageMethod problem...

    05-05-2008, 7:22 PM

    I'm still in the learning process about this stuff, so I might just have something wrong in my head, but I just can't understand why this isn't working.

    I have the following javascript on a .aspx page:

            function NewActivity(actID)
            {
                PageMethods.InsertActivity(onActivityChanged, OnWSRequestFailed);
            }
            
            function onActivityChanged(result)
            {
                if (result != null)
                {
                    $get("divActivitiesRepeater").innerHTML = result;
                }
            }
            
            function OnWSRequestFailed(error) {
                alert("Stack Trace: " + error.get_stackTrace() + "\r\n" +
                "Error: " + error.get_message() + "\r\n" +
                "Status Code: " + error.get_statusCode() + "\r\n" +
                "Exception Type: " + error.get_exceptionType() + "\r\n" +
                "Timed Out: " + error.get_timedOut()); }
    And the following in that pages class (C#):
    [WebMethod(true)]
    public static string InsertActivity()
    {
    return "Hello";
    }
     
    Whenever I go to my page and perform an action which calls the NewActivity() javascript function, I get an 'Authentication Failed' error. Now it isn't as if I'm doing anything fancy here. 
    I've tried messing about with the 'WebMethod' attribute, making it [WebMethod(EnableSession=true)] but no difference. Is there something I'm missing? Or is there something else I could have set in my application that could be causing this error?
  • Re: WebMethod / PageMethod problem...

    05-06-2008, 8:26 AM
    • Loading...
    • gonzo11
    • Joined on 07-02-2007, 4:08 PM
    • Posts 122

    Hi,

     

    Did you set the EnablePageMethods property to your ScriptManager to true ?

     

    hope this helps

     

    regards,

    G

  • Re: WebMethod / PageMethod problem...

    05-07-2008, 12:25 AM

    I knew there was something I forgot to mention! Yes, I have that set on my scriptmanager, but no luck. 

  • Re: WebMethod / PageMethod problem...

    05-07-2008, 3:56 AM
    • Loading...
    • stmarti
    • Joined on 06-06-2006, 12:20 PM
    • Posts 539
    Have you tried
    [WebMethod] instead [WebMethod(true)]
    ?
    You can also try to add some dummy parameter to the method, like: public static string InsertActivity( string dummy ), it would be weird if helps...
    You can check whether your pagemethod is available on the client site, open the source view in the browser and look for "PageMethods" 
  • Re: WebMethod / PageMethod problem...

    05-07-2008, 7:04 PM

    Thanks for your suggestions, but no none of that helps. I initially was running it with a parameter, but then I took it out to make testing easier to work through. I firstly only had [WebMethod], but many people suggested adding the (true) parameter in there. Didn't help. Yes, the page method is available. If it wasn't available, it would be giving me a 'object undefined' error or something similar, opposed to an 'Authentication Failed' error. Here is the rendered javascript to do with page methods anyway:

     

    <script type="text/javascript">
    <!--
    var PageMethods = function() {
    PageMethods.initializeBase(this);
    this._timeout = 0;
    this._userContext = null;
    this._succeeded = null;
    this._failed = null;
    }
    PageMethods.prototype = {
    InsertActivity:function(dummy,succeededCallback, failedCallback, userContext) {
    return this._invoke(PageMethods.get_path(), 'InsertActivity',false,{dummy:dummy},succeededCallback,failedCallback,userContext); }}
    PageMethods.registerClass('PageMethods',Sys.Net.WebServiceProxy);
    PageMethods._staticInstance = new PageMethods();
    PageMethods.set_path = function(value) { 
    var e = Function._validateParams(arguments, [{name: 'path', type: String}]); if (e) throw e; PageMethods._staticInstance._path = value; }
    PageMethods.get_path = function() { return PageMethods._staticInstance._path; }
    PageMethods.set_timeout = function(value) { var e = Function._validateParams(arguments, [{name: 'timeout', type: Number}]); if (e) throw e; if (value < 0) { throw Error.argumentOutOfRange('value', value, Sys.Res.invalidTimeout); }
    PageMethods._staticInstance._timeout = value; }
    PageMethods.get_timeout = function() { 
    return PageMethods._staticInstance._timeout; }
    PageMethods.set_defaultUserContext = function(value) { 
    PageMethods._staticInstance._userContext = value; }
    PageMethods.get_defaultUserContext = function() { 
    return PageMethods._staticInstance._userContext; }
    PageMethods.set_defaultSucceededCallback = function(value) { 
    var e = Function._validateParams(arguments, [{name: 'defaultSucceededCallback', type: Function}]); if (e) throw e; PageMethods._staticInstance._succeeded = value; }
    PageMethods.get_defaultSucceededCallback = function() { 
    return PageMethods._staticInstance._succeeded; }
    PageMethods.set_defaultFailedCallback = function(value) { 
    var e = Function._validateParams(arguments, [{name: 'defaultFailedCallback', type: Function}]); if (e) throw e; PageMethods._staticInstance._failed = value; }
    PageMethods.get_defaultFailedCallback = function() { 
    return PageMethods._staticInstance._failed; }
    PageMethods.set_path("/EPO/Project/Prepare/Activities/Default.aspx");
    PageMethods.InsertActivity= function(dummy,onSuccess,onFailed,userContext) {PageMethods._staticInstance.InsertActivity(dummy,onSuccess,onFailed,userContext); }
    function WebForm_OnSubmit() {
    return ValidateTabs();if (typeof(ValidatorOnSubmit) == "function" && ValidatorOnSubmit() == false) return false;
    return true;
    }
    // -->
    </script>
     
  • Re: WebMethod / PageMethod problem...

    05-08-2008, 3:21 AM
    Answer
    • Loading...
    • stmarti
    • Joined on 06-06-2006, 12:20 PM
    • Posts 539

    I still have some tips Smile:

    - test/debug with vs built in webserver, not iis. Maybe this is an iis config problem.

    - test/debug with a local iis/or the built in webserver (remote install problem)

    - create a default web app/web site and test whether the pagemethod works there (not works: corrupted install(?), works: you app config is incorrect)

    - use fiddler to test what happens (works both with ie and firefox), or use firebug for the same task
      

  • Re: WebMethod / PageMethod problem...

    05-08-2008, 8:01 PM

    I am only running this through the built in VS webserver, as it is still in development stages.

    I tried creating a new web project as you suggested, and putting an identical pagemethod into it, and it ran perfectly.

    I had a look at what happens in fiddler, and the only difference between the one that doesnt work, and my blank test one that does is a couple of cookie values in the header. I disabled authentication on the application, and whatever else to get rid of those cookies, and it made no difference. Fiddler didn't give me any further information than 'Authentication Failed' either.

    I'm really not sure what else I can look for... I don't understand why it needs to authenticate somebody at all, and why it wouldn't be getting through. I do use identity impersonation, but even after switching it off, I'm still getting the same problem. 

  • Re: WebMethod / PageMethod problem...

    05-09-2008, 1:47 AM

    Just as another note. I tried creating a new blank page, with no masterpage, and its own scriptmanager within the same project. I set up an identical scenario with this new page, and it worked perfectly fine. So it isn't a web.config setting, as they are applied to this page as well of course. It is obviously something in my page / code that is causing this to be such a pain. I really don't know what it could be that is stopping it from working.

  • Re: WebMethod / PageMethod problem...

    05-09-2008, 2:09 AM
    Answer

    I got it!

    I learned from all this that a pagemethod is called by a url (the url of the current page, minus the query string, with a slash "/" and the methods name tacked on to the end). The page I am trying to run it on has a requirement that one of its querystring values is initialised, and if it is not, then it is redirected. Obviously, this is enough to disallow the pagemethod from running, so I just gave an exception to any url that contains the string ".aspx/", and all is good! Thanks to you for helping me out, even if I did figure this out myself. 

Page 1 of 1 (9 items)