1 Reply Latest reply on Dec 6, 2007 6:33 PM by ntsiii

    Communicating with a webservice through an Action Script file

    VitaltypeMike
      I am trying to communicate with a webserivce through an Actionscript class. I wanted to first here any ideas or suggestions on whether this considered a typical industry practice. I am new to Flex 2 and Actionscript 3. I am trying to test a simple form that creates a new user. The webservice has been tested and returns either "Success" or "Duplicate". Based on those results, I want a label on the form to display a more informative message. I believe I am having two problems.

      1) The first is communicating with the MXML and the Actionscript class. Based on my research, I decided to use a local function within the script block to communicate with the Actionscript classfile. I can't figure out how to handle the result from the Action class and update the label with the informative message. I have the informative messages coming from the Actionscript class.

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="vertical" xmlns:asScript="AS.*">
      <mx:Script>
      <![CDATA[
      import AS.newUser;
      import AS.Users;
      [Bindable]
      public function newTTEUser()
      {
      var UserName:String = tbxUserName.text;
      var Password:String = tbxPassword.text;
      var Email:String = tbxEmail.text;
      var FirstName:String = tbxFirstName.text;
      var LastName:String = tbxLastName.text;
      var phone:String = tbxPhone.text;
      var role:String = Roles.selectedValue.toString();
      AS.newUser(UserName, Password, Email, FirstName, LastName, phone, role);
      lblResult.text = AS.newUser.lastResult
      }
      ]]>
      </mx:Script>

      <mx:Panel width="308" height="343" layout="absolute" id="pnlNewUser" title="New User" horizontalScrollPolicy="off" verticalScrollPolicy="off">
      <mx:Form x="10" y="0" width="100%" height="185" horizontalScrollPolicy="off" verticalScrollPolicy="off">
      <mx:FormItem label="UserName:">
      <mx:TextInput id="tbxUserName"/>
      </mx:FormItem>
      <mx:FormItem label="Password:">
      <mx:TextInput id="tbxPassword"/>
      </mx:FormItem>
      <mx:FormItem label="Email:">
      <mx:TextInput id="tbxEmail"/>
      </mx:FormItem>
      <mx:FormItem label="First Name:">
      <mx:TextInput id="tbxFirstName"/>
      </mx:FormItem>
      <mx:FormItem label="Last Name:">
      <mx:TextInput id="tbxLastName"/>
      </mx:FormItem>
      <mx:FormItem label="Phone:">
      <mx:TextInput id="tbxPhone"/>
      </mx:FormItem>
      <mx:FormItem>
      </mx:FormItem>
      </mx:Form>
      <mx:Button x="10" y="267" label="Submit" id="btnSubmit" click="newTTEUser()" />
      <mx:RadioButtonGroup id="Roles"/>
      <mx:RadioButton x="110.5" y="193.0" label="Typist" groupName="Roles"/>
      <mx:RadioButton x="110.5" y="219" label="Proofer" groupName="Roles"/>
      <mx:RadioButton x="110.5" y="245" label="Admin" groupName="Roles"/>
      <mx:Label x="55" y="219" text="Role:" fontWeight="bold"/>
      <mx:Label x="84" y="269" width="170.5" visible="true" id="lblResult"/>
      </mx:Panel>
      </mx:Application>

      The second problem is handling the result from the webservice and sending the information to the MXML file. I believe I have the If/Else statement right. I am not sure if I use a bindable variable or some type of result event.

      package AS
      {
      [Bindable]
      public function newUser(
      userName:string,
      password:string,
      email:string,
      firstName:string,
      lastName:string,
      phone:string,
      role:string):String
      {
      var wsNewTTEUser:WebService = new WebService();
      wsNewTTEUser.wsdl = " http://localhost:3242/TTEIntranet/Users.asmx?WSDL";
      wsNewTTEUser.loadWSDL();
      var Result:String = wsNewTTEUser.newUser(userName, password, email, firstName, lastName, phone, role);
      if (result = "Success")
      {
      return "The new user has been created!!";
      }
      else
      {
      return "The username already exists";
      }
      }
      }

      I appreciate any comments or help of the problems I am having. Thanks, Mike
        • 1. Re: Communicating with a webservice through an Action Script file
          ntsiii Level 3
          All Flex data service calls are asynchronous. You can never access the result in the same function in which you make the call. so this will never work:
          AS.newUser(UserName, Password, Email, FirstName, LastName, phone, role);
          lblResult.text = AS.newUser.lastResult

          Nor will:
          var Result:String = wsNewTTEUser.newUser(userName, password, email, firstName, lastName, phone, role);

          You must use binding, or better, a result handler function.

          wsNewTTEUser.resultFormat = "e4x"
          wsNewTTEUser.addEventListener("REsultEvent", onResult);

          private function onResult(event:ResultEvent):void {
          var xmlResult:XML = XML(event.result);
          trace(xmlResult.toXMLString());

          Tracy