4 Replies Latest reply on Mar 11, 2010 8:04 AM by alice_data

    How Can You Tell if the URLRequest Variables Are Posted?

    alice_data Level 1

      Hi,

       

      This sounds really odd, because I happen to have three variables I am supposed to pass to an httpservice and have the results output to my Flex app.

       

      The problem is, I don't know how I can tell if a variable has been posted or not, because no matter what I do, it looks like my trace statement is accurate, but there is nothing shown on the screen.

       

      I tried changing it to GET, and then I get this IO Event Handler error, because my variables are so long.

       

      var loader:URLLoader = new URLLoader();
              var url:String = "http://localhost/generic.php";
              var variables:URLVariables = new URLVariables();
                  variables.from= from_string;
                  variables.state_colors = state_colors_string;
                  variables.change = change_string;                       
              var encode:String= encodeURI(url);
              var pattern:RegExp = /#/g;
              var decode:String = encode.replace(pattern,"%23");
              var request:URLRequest = new URLRequest(decode);           
              request.method = URLRequestMethod.POST;          

       

      I am wondering if my variables have been passed correctly. Is there some other way other than using trace statements?

       

      Thanks for your help.

        • 1. Re: How Can You Tell if the URLRequest Variables Are Posted?
          Michael Hovey Level 1

          For one thing, you don't need to do the encoding yourself, URLRequest and URLLoader will handle that for you. Here's how I like to structure my URLVariables/URLRequest/URLLoader functions:

           

          var params:URLVariables = new URLVariables();

          params.from = from_string;

          params.state_colors = state_colors_string;

          params.change = change_string;

           

          var request:URLRequest = new URLRequest(url);

          request.data = params;

          request.method = URLRequestMethod.POST;

           

          var loader:URLLoader = new URLLoader();

          loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);

          loader.addEventListener(IOErrorEvent.IO_ERROR, onIOError);

          loader.addEventListener(Event.COMPLETE, onComplete);

          loader.load(request);

           

          Setting the URLRequest's url and data properties will correctly encode everything for you. As for the best trace method, I'm not exactly sure if there's a built in one that will show everything you need. Sorry if this doesn't exactly answer your question but thought I'd throw it out there.

          • 2. Re: How Can You Tell if the URLRequest Variables Are Posted?
            alice_data Level 1

            Hi,

             

            Thanks for the tip, and I have just done that with my php code end. It turns out that the file was generated, and the file is blank after the code has finalized execution.

              So, I guess that my POST variables is definitely not posted.

             

            By the way, I have added a couple of lines for security and IO checks, like you suggested, and here is the almost complete code:

             

            public function changeMap():void{                        
                      
                
                    var url:String = "http://localhost/generic.php";
                    var variables:URLVariables = new URLVariables();
                        variables.from= from_string;
                        variables.state_colors = state_colors_string;
                        variables.change = change_string;
                    var encode:String= encodeURI(url);
                    var pattern:RegExp = /#/g;
                    var decode:String = encode.replace(pattern,"%23");
                    var request:URLRequest = new URLRequest(decode);           
                    request.method = URLRequestMethod.POST;       
                   
                    var loader:URLLoader = new URLLoader();
                    loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
                    loader.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
                    loader.addEventListener(Event.COMPLETE, onLoadComplete);   
                    loader.load(request);                    
                    }       
                     private function onSecurityError(event:SecurityErrorEvent):void {
                        trace("securityErrorHandler: " + event);
                    }
                     private function onIOError(event:IOErrorEvent):void {
                        trace("ioErrorHandler: " + event);
                    }

             

                    public function onLoadComplete(event:Event):void {             //event output                             
                    }         

             

            From the console, the error events returned nothing from the addEventListeners.

             

            When I used GET before, they worked with two passing variables, but since my variables are now more than 512 characters, I cannot put this on the url, which is why I am insisting on using POST here.

             

            Have I done anything else wrong here?

             

            Thanks for your help.

            • 3. Re: How Can You Tell if the URLRequest Variables Are Posted?
              Sam Souhrada Level 1

              Here is a basic working example that shows you how to send data back/forth from Flex to PHP.

               

               

              http://www.riaindy.com/adobe/2010/02/adobe-flex-php-integration/

              • 4. Re: How Can You Tell if the URLRequest Variables Are Posted?
                alice_data Level 1

                Hi,

                 

                Thanks for the example.

                Just as I am writing this, I found that I have forgotten this line in my code:   request.data= variables; Adding that line has surely performed the trick.

                 

                Thanks.

                 

                Alice