10 Replies Latest reply on Dec 22, 2007 10:45 AM by nak5ive

    HTTPService and POST variables

    elitecodex
      I have a sample application that Im running from within Flex Builder. I make the request to a PHP page. Just as an example, I am using var_dump($_POST); just because I want to see if the variables are getting sent. Its not.

      I cannot figure out how to do this... I've searched the web and followed all the models I have found - nothing appears to work. Can someone help me on this?

      Here is some code from my application - perhaps it will help.

      <mx:HTTPService id="loginService" method="POST" url=" http://localhost/v2/index.php?module=login" resultFormat="xml" useProxy="false" >
      <mx:request xmlns="">
      <username>
      {username1.text}
      </username>
      <password>
      {password1.text}
      </password>
      </mx:request>
      </mx:HTTPService>


      I have a button that calls this when pressed.

      public function login():void
      {

      //mx.controls.Alert.show("USername = " + u + " and Password = " + p, "Login Debug");
      loginService.addEventListener(ResultEvent.RESULT, this.loginResult);
      loginService.addEventListener(FaultEvent.FAULT, this.loginFault);

      loginService.send();
      }


      Any ideas?? Im lost!
        • 1. Re: HTTPService and POST variables
          xrum Level 2
          hi :)

          this is how i'm doing it. not sure if it's what you need, but might get you on the right path.....

          [code]
          private function RunReport():void
          {
          var url:String = " http://myurl.com
          var u:URLRequest = new URLRequest();
          var variables:URLVariables = new URLVariables();

          variables.FY=lstFY.selectedItem.@id;
          variables.USERNAME=lstUSERNAME.selectedItem.@id;
          variables.PASSWORD=lstPASSWORD.selectedItem.@id;
          variables.STATUS=lstStatus.selectedItem.@id;
          u.data = variables;
          u.url = url;
          u.method = "POST";
          navigateToURL(u,"_blank");
          }
          [/code]

          good luck! :)
          • 2. Re: HTTPService and POST variables
            elitecodex Level 1
            Thats close to what I want.... however I dont want to change the page. I want to send the request and parse the returning XML for processing in the same app. Is there a way I can do that with the URLRequest?
            • 3. Re: HTTPService and POST variables
              ntsiii Level 3
              I think you are better off with HTTPService.

              First, set resultFormat="e4x". That is the "real" xml. resultFormat="xml" is the obsolete AS2 xml, now known as xmlDocument. Don't use it.

              Then, instead of building the request object in mxml, use AS, becuse it is much easier to debug. As you have seen, with binding, you have difficult seeing what is wrong. I'll post some snippets when I get to the machine that has them.

              Also, unless you are extremely concerned about memory usage, and you ae sure to remove the listeners in te result handler, it is easier to declare the result handler in athe HTTPService tag.
              Tracy
              • 4. Re: HTTPService and POST variables
                elitecodex Level 1
                ntsiii - Thanks for the reply. I'll make the changes you suggested and await for the snippet you were talking about.
                • 5. Re: HTTPService and POST variables
                  ntsiii Level 3
                  Here are those snippets. You can ignore the AsyncToken stuff for now, but you will want it later.

                  Tracy

                  Sample code using HTTPService, e4x, handler function to populate a list item.
                  Also shows usage of AsyncToken.

                  The DataGrid tag:
                  <mx:DataGrid id="dg" dataProvider="{_xlcMyListData}" .../>


                  The HTTPService tag:
                  <mx:HTTPService id="service" resultFormat="e4x" result="onResult(event)" fault="..../>

                  Script block declaration:
                  import mx.rpc.Events.ResultEvent;
                  [Bindable]private var _xlcMyListData:XMLListCollection;

                  Invoke send:
                  var oRequest:Object = new Object();
                  oRequest.Arg1 = "value1";
                  var callToken:AsyncToken = service.send(oRequest);
                  token.callId = "myQuery1";

                  Result Handler function:
                  private function onResult(oEvent:ResultEvent):void {
                  var xmlResult:XML = XML(event.result); //converts result Object to XML. can also use "as" operator
                  var xlMyListData:XMLList = xmlResult.myListData; //depends on xml format, is row data
                  _xlcMyListData = new XMLListCollection(xlMyListData); //wrap the XMLList in a collection
                  trace(_xlcMyListData.toXMLString()); //so you can see exactly how to specify dataField or build labelFunction
                  var callToken:AsyncToken = oEvent.token;
                  var sCallId = callToken.callId; //"myQuery1"
                  switch(sCallId) { //Process the result conditionally
                  case "myQuery1":
                  doQuery2(); //do whatever. this example calls another data service query
                  break;
                  ...
                  }
                  }//onResult
                  • 6. HTTPService and POST variables
                    elitecodex Level 1
                    I tried to change it based on what you suggested with no luck. Still not sending any post variables.

                    Here is what I have now.

                    <mx:HTTPService
                    id="loginService"
                    method="POST"
                    url=" http://localhost/v2/index.php?module=login&amp;lib=ajax"
                    resultFormat="e4x"
                    result="loginResult(event)"
                    fault="loginFault(event)"
                    useProxy="false"/>

                    and the loginResult() method

                    public function loginResult(evt:ResultEvent):void
                    {
                    //Alert.show("Result = " + evt.message);
                    try
                    {
                    var resultDoc:XMLDocument = XMLUtil.createXMLDocument(evt.message.body.toString());

                    var firstNodeStr:String = resultDoc.firstChild.firstChild.nodeName;

                    if ( firstNodeStr == "fault" )
                    {
                    Alert.show("Fault received \""+resultDoc.firstChild.firstChild.firstChild.nodeName+"\"");
                    errorLabel.text = resultDoc.firstChild.firstChild.firstChild.firstChild.nodeValue;
                    errorLabel.visible = true;
                    }
                    else
                    {
                    Alert.show("firstNodeStr = " + firstNodeStr);
                    }
                    }catch(errorObj:Error)
                    {
                    errorLabel.text = "FATAL: " + errorObj.message;
                    errorLabel.visible = true;
                    }
                    }

                    and the login() method which calls the send() method on the httpservice.

                    public function login():void
                    {
                    var request:Object = new Object();
                    request.username = this.username1.text;

                    loginService.send(request);
                    }

                    Im close to giving up... I dont understand why sending post variables are so hard. Am I missing something completely?? Is there a way I can debug this? How about another way to go about doing it? I dont mind doing extra AS coding... just want to get the functionality down.

                    Thanks for all the help. I hope more ideas come through :)
                    • 7. Re: HTTPService and POST variables
                      ntsiii Level 3
                      First, why are you doing:
                      var resultDoc:XMLDocument = XMLUtil.createXMLDocument(evt.message.body.toString());
                      That makes no sense at all.

                      Next, for debugging, hard code the request parameter:
                      request.username = "ABogusTestValue;

                      See if that comes through.

                      If it doesn't, then I suspect a problem on the server side. You can get a "sniffer" that will let you see exactly what is being sent back and forthe over the HTTP. I recall a lot of people use "Charles"

                      Tracy

                      • 8. Re: HTTPService and POST variables
                        elitecodex Level 1
                        I did that so I could get access to it via a XMLDocument since thats what I was using - no specific reason. But that part works ok...

                        The problem is that the server is not receiving any post variables. Like I said above, Im doing a var_dump($_POST) and nothing is being displayed (an empty array). So php is not getting any variables. I used the same script with HTML forms and can verify that the server is operational.

                        I can see the requests going over the network with Firebug and its responses. Im responding properly (with an xml document - hence the statement above). $_GET is working great, but its not sending any $_POST. Thats what Im trying to do.

                        Ive already tried hardcoding test values and it gives me the same thing.
                        • 9. Re: HTTPService and POST variables
                          ntsiii Level 3
                          No, you are mistaken about XMLdocument. But whatever.

                          Did you try using hardcoced values recently?

                          Create a test app, with nothing in it but the HTTPService code.

                          Tracy
                          • 10. Re: HTTPService and POST variables
                            nak5ive
                            i've been having tons of problems with this as well.
                            i noticed that i was getting empty $_POST arrays in my php files as well.
                            however, for some reason, if i killed all the warnings in php about not having the post variables defined within my scripts, somehow it magically worked!
                            to further investigate, i use wireshark to see what kind of data is being passed along to the php script from the flex app. to my suprise, the data was not being sent via HTTP at all. in fact several seemingly random ports were used each time, all falling within the accepted TCP port range (15##).
                            it almost seemed like the data was being sent in a binary form even though is was simply text.
                            anyhow, as a control to my experiment, a simple html form posted variables as expected, with a very obvious POST entry seen in Wireshark. So far i have not been successful in getting the same results from flex.

                            temporary solution... in your php script, kill all warnings by placing a '@' symbol in front of each statement. you might be suprised that it actually does work.