6 Replies Latest reply on Mar 2, 2007 10:52 AM by Chicerjunge

    Webservices inside functions

    Chicerjunge Level 1
      I have a webservice created with just an id and wsdl using the mx:WebService. Whenever I make a button to call a method from the web service I can use the bindalbe last result with no problem. However, when I call the method from within a function, lastResult is always null. The function, a cfc function, merely returns a string "test".


      //the function below never has anything in lastResult (looking at debugging and a billion other ways of testing. I've simplified below

      private function test():String {
      newService.testCFC();
      var myString:String = newService.testCFC.lastResult
      return myString;
      }

      <mx:WebService id="newService" wsdl=" http://localhost:8500/com/controller/rpController.cfc?wsdl"/>

      //This works just fine. testCFC is called when the button is clicked and the label shows up
      <mx:Button label="{newService.testCFC.lastResult}" click="newService.testCFC()"/>
        • 1. Re: Webservices inside functions
          babo_ya Level 3
          I think you have to wait till a complete event or something.....

          newService.testCFC.addEventListener(ResultEvent, loadComplete);
          newService.testCFC();

          public function loadComplete(ResultEvent event):void
          {
          event.result;
          }

          I just typed these here so, probably will not work.

          Thanks,
          • 2. Re: Webservices inside functions
            Chicerjunge Level 1
            so I can't directly call a webservice. Is this due to the 'asynchronous' processing I hear about?
            • 3. Re: Webservices inside functions
              batmitra
              hi
              if you want to call a webservice and handle the result on a function, you must use another aproach:

              1st - The webservice have a property called RESULT where you can pass the function that handles your service result.

              2nd- The webservices always return an event, so you need to control this event when it answers, thats why you get null from lastresult

              3rd- The function to handle the event, usually doesn't return a specific type, so you must use Void for that

              Here is a sample code:

              Your function stays inside this tag
              <mx: script>

              private function MyResultHandler(event:ResultEvent):Void{

              var mystring:string = event.result.toString;

              }

              </mx:script>
              Your webservice must be like this:
              <:mx:webservice id="yourservicename" wsdl="yourservicelocaion" result="Your result function handler >
              <mx:operation name="youroperationname">

              </mx:operation>
              </:mx:webservice>

              then when you click the button you do like this
              <:mx:button label="yourlabel" click="yourwebservicename.youroperation.send()"/>

              this should work
              about the code probably there are mistakes in case sensitive etc, but the idea is there
              • 4. Re: Webservices inside functions
                Chicerjunge Level 1
                Thank you. I seem to understand all that now. I still have one question regarding the ActionScript. Is it possible to have a single function that instantiates a webservice, calls a method, and returns the value from the method?

                I was trying to avoid the <mx:webservice>
                • 5. Re: Webservices inside functions
                  theLoggerGuy Level 1
                  quote:

                  Thank you. I seem to understand all that now. I still have one question regarding the ActionScript. Is it possible to have a single function that instantiates a webservice, calls a method, and returns the value from the method?

                  I was trying to avoid the <mx:webservice>


                  Well you probably could, but you'd be fighting the way the language works. It's event driven which is the asynchronous part, ie., can be executed in any order. "batmitra" has described it well and I would suggest that you follow that model.

                  Also you don't need to use <mx:webservice> You can call it in a method like so:

                  import mx.rpc.soap.WebService;
                  var webService:WebService = new WebService( );
                  webService.wsdl = " http://localhost:8500/com/controller/rpController.cfc?wsdl";
                  webService.loadWSDL( );
                  webService.addEventListener(LoadEvent.LOAD, processWSDL);

                  which will call the method "processWSDL" when the service is complete. You can then check for errors and process the results. If you are convinced you want to do it one function then I guess you could try to call yourself, but I haven't done that and don't know how it will work.
                  • 6. Re: Webservices inside functions
                    Chicerjunge Level 1
                    I think the source of my confusion was not necessarily calling the webservice using the mxml, but the how the results are received.

                    My confusion was that the results of the webservice were not available to me whenever I was processing the next set of instructions. So, now I see that I have to ensure that all code that requires the web service data executes after the resultevent from the web service.

                    Thanks everyone