6 Replies Latest reply on Jul 5, 2007 11:03 AM by ntsiii

    HTTPService Request Issues

    mukul.seth
      I am new to Flex, and I'm trying to setup a PHP backend which retrieves data from a MYSQL database. If I use the GET method by including the parameters right in the URL. But if I try to use the <mx:request /> (GET or POST) object inside the HTTPService, the parameters are not received correctly, and the XML returned is empty. My MXML code is below. Sorry about the crappy code formatting. I couldn't figure out to enter formatted code.

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute">
      <mx:Script>
      <![CDATA[

      import mx.collections.*;
      import mx.rpc.events.*;
      import mx.controls.*;

      [Bindable]
      private var contacts:Object;

      [Bindable]
      private var username:String = "mseth";

      private function contactHandler(evt:ResultEvent):void {
      contacts = myService.lastResult.contacts.contact;
      }

      private function faultHandler(evt:FaultEvent):void {
      var faultMessage:String = "Could not connect with XML file";
      Alert.show(faultMessage, "Error Opening File");
      }

      ]]>
      </mx:Script>
      <mx:HTTPService id="myService" contentType="application/xml" method="GET" url=" http://localhost/getContacts.php" result="contactHandler(event)" fault="faultHandler(event)" >
      <mx:request xmlns="">
      <user>"{uName.text}"</user>
      <fName>"{fName.text}"</fName>
      <lName>"{lName.text}"</lName>
      </mx:request>
      </mx:HTTPService>
      <mx:VBox>
      <mx:Form>
      <mx:FormItem label="Username:">
      <mx:TextInput id="uName" text="mseth"/>
      </mx:FormItem>
      <mx:FormItem label="First Name:">
      <mx:TextInput id="fName"/>
      </mx:FormItem>
      <mx:FormItem label="Last Name:">
      <mx:TextInput id="lName"/>
      </mx:FormItem>
      <mx:FormItem>
      <mx:Button label="Search" click="myService.send()"/>
      </mx:FormItem>
      </mx:Form>
      <mx:DataGrid dataProvider="{contacts}" width="100%">
      <mx:columns>
      <mx:DataGridColumn dataField="id" headerText="ID" />
      <mx:DataGridColumn dataField="first_name" headerText="First Name" />
      <mx:DataGridColumn dataField="last_name" headerText="Last Name" />
      <mx:DataGridColumn dataField="username" headerText="User" />
      <mx:DataGridColumn dataField="email" headerText="Email Address" />
      <mx:DataGridColumn dataField="home_phone" headerText="Home" />
      <mx:DataGridColumn dataField="cell_phone" headerText="Cell" />
      <mx:DataGridColumn dataField="office_phone" headerText="Office" />
      </mx:columns>
      </mx:DataGrid>
      <mx:TextArea text="{username}" />
      </mx:VBox>
      </mx:Application>

      I would appreciate any help you can give me.
        • 1. Re: HTTPService Request Issues
          Archades Level 1
          I dont have a lot of time to look through your code but here is some code that I have that will load up some xml that could just as easily get it from a php script if you set the "method" attribute in the HTTPService tag.

          This code should have every sniplet you need.

          <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" initialize="appInit()" >

          private var factions:XMLList;
          public function appInit():void
          {
          faction_data.send();
          }
          //***************************************//
          private function requestFault(e:FaultEvent):void
          {
          trace( "requestFault" + faction_data.lastResult);
          // TODO: dont output the entire message when it goes public //
          Alert.show("Problem reading file : \n" + e.message, "HTTPService Error");
          }

          //***************************************//
          private function requestResult(e:ResultEvent):void
          {
          factions = faction_data.lastResult.faction;
          trace("reqestResult: \n" + factions);
          }

          <mx:HTTPService id="faction_data" resultFormat="e4x" url="factions.xml" fault="requestFault(event)" result="requestResult(event)" />
          • 2. Re: HTTPService Request Issues
            mukul.seth Level 1
            Thanks Archades.

            I have already tried something similar to what you recommended, and it works that way. But what I'm trying to do is send some info to the php script, using the <mx:request> tag, and based on that info the php script returns the data in XML format. The problem I'm having is that the data inside the request tag is not being received properly. Essentially, I'm looking for something in this format -

            <mx:HTTPService id="myService" contentType="application/xml" method="POST" url=" http://localhost/getContacts.php" result="contactHandler(event)" fault="faultHandler(event)" >
            <mx:request xmlns="">
            <user>"{uName.text}"</user>
            <fName>"{fName.text}"</fName>
            <lName>"{lName.text}"</lName>
            </mx:request>
            </mx:HTTPService>

            Where user, fName, lName are the parameters being sent to the php script.
            • 3. Re: HTTPService Request Issues
              ntsiii Level 3
              I never use the declaritive syntax for this, though it should work. It is just too hard to debug.

              Instead, do as Archades suggests, but build your request parameters like this:
              var oRequest:Object = new Object();
              oRequest.user = uName.text;
              oRequest.fname = fName.text;
              ...

              myService.send(oRequest);

              This way you can debug the values of the rrequest object before it is sent.

              Tracy
              • 4. Re: HTTPService Request Issues
                mukul.seth Level 1
                Thanks Tracy, that seems like the best option... I will try it out.
                • 5. Re: HTTPService Request Issues
                  dave cragg Level 2
                  Inside the request tags, I don't think you should use quotes. Try this instead:

                  <mx:request>
                  <user>{uName.text}</user>
                  <fName>{fName.text}</fName>
                  <lName>{lName.text}</lName>
                  </mx:request>

                  • 6. Re: HTTPService Request Issues
                    ntsiii Level 3
                    Yes, Dave is correct.
                    Tracy