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

    HTTPService Request Issues

      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">

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

      private var contacts:Object;

      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:HTTPService id="myService" contentType="application/xml" method="GET" url=" http://localhost/getContacts.php" result="contactHandler(event)" fault="faultHandler(event)" >
      <mx:request xmlns="">
      <mx:FormItem label="Username:">
      <mx:TextInput id="uName" text="mseth"/>
      <mx:FormItem label="First Name:">
      <mx:TextInput id="fName"/>
      <mx:FormItem label="Last Name:">
      <mx:TextInput id="lName"/>
      <mx:Button label="Search" click="myService.send()"/>
      <mx:DataGrid dataProvider="{contacts}" width="100%">
      <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:TextArea text="{username}" />

      I would appreciate any help you can give me.
        • 1. Re: HTTPService Request Issues
          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
          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="">

            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;


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

              • 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:


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