2 Replies Latest reply on Nov 14, 2006 2:14 PM by dr_ross

    Data from ResultEvent

    dr_ross Level 1
      hi all,
      I'm having trouble referencing data retrived via HTTPService listener and ResultEvent.
      The DataGrid i've plugged into the HTTPService works fine, but i can't seem to access the individual variables inside the lastResult object properly.

      Heres my code:

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml"
      xmlns:control="controller.*"
      xmlns:forms="forms.*"
      layout="vertical" creationComplete="userRequest.send()">
      <mx:Style>
      Label {
      fontSize:14;
      }
      </mx:Style>
      <mx:Script>
      <![CDATA[

      import mx.core.Application;
      import events.LoginEvent;
      import mx.rpc.events.ResultEvent;
      import mx.rpc.soap.WebService;
      import mx.rpc.http.HTTPService;
      import mx.controls.Alert;
      import mx.rpc.events.FaultEvent;
      import flash.net.URLLoader;
      import flash.net.URLRequest;
      import flash.net.URLVariables;
      import flash.events.Event;


      public function checkUser():void
      {
      userRequest.send();

      userRequest.addEventListener("result",checkUser_listener);

      }

      private function checkUser_listener(event:ResultEvent):void
      {
      Alert.show(userRequest.lastResult.users.user.username);
      }
      ]]>
      </mx:Script>
      <control:FrontController/>
      <forms:NewUserForm/>
      <mx:HTTPService id="userRequest" url="check1.php" useProxy="false" method="POST">
      <mx:request xmlns="">
      <username>{username.text}</username><password>{password.text}</password><emailaddress>{em ailaddress.text}</emailaddress>
      </mx:request>
      </mx:HTTPService>
      <mx:Form x="22" y="10" width="493" height="331">
      <mx:HBox>
      <mx:Label text="Username"/>
      <mx:TextInput id="username"/>
      </mx:HBox>
      <mx:HBox>
      <mx:Label text="Password"/>
      <mx:TextInput id="password"/>
      </mx:HBox>
      <mx:HBox>
      <mx:Label text="Email Address"/>
      <mx:TextInput id="emailaddress"/>
      </mx:HBox>
      <mx:Button label="Submit" click="checkUser()"/>
      <mx:DataGrid id="dgUserRequest" dataProvider="{userRequest.lastResult.users.user}">
      <mx:columns>
      <mx:DataGridColumn headerText="User ID" dataField="userid"/>
      <mx:DataGridColumn headerText="User Name" dataField="username"/>
      <mx:DataGridColumn headerText="Password" dataField="password"/>
      <mx:DataGridColumn headerText="Email" dataField="emailaddress"/>
      </mx:columns>
      </mx:DataGrid>
      <mx:TextInput id="selectedemailaddress" text="{dgUserRequest.selectedItem.emailaddress}"/>
      </mx:Form>
      <mx:Text text="v1.28"/>
      </mx:Application>

      As you can see in the "checkUser_listener" function i'm trying to acces the username variable, used fine in the dataGrid, but i can't seem to get the syntax right. Any help would be much appreciated. Cheers, Dr_ross
        • 1. Re: Data from ResultEvent
          ntsiii Level 3
          First, using addListener is ok, but mor complicated than necessary. As you have it, there is no listener when the service is called the first time, on creationComplete, and I am pretty sure it is too late to have if after the send in the checkUser() function.

          Instead just specify the handler in the HTTPService tag:
          result="checkUser_listener(event)" (note the "event" arg, it is critical)
          That takes care of that once and for all.

          Second, specify resultFormat="e4x". I think the default is mx:OBject and is harder to work with that xml.

          Third, don't use lastResult in a handler, it is for binding expressions only.

          Instead do:
          private function checkUser_listener(event:ResultEvent):void
          {
          var xmlResult:XML = XML(event.result);
          Alert.show(xmlResult.toXMLString()); //you shuld see your xml.
          }

          Tracy
          • 2. Re: Data from ResultEvent
            dr_ross Level 1
            you sir/madam are a star, thanks for that. works like a charm, thanks for the help.