4 Replies Latest reply on Nov 13, 2006 12:27 PM by ntsiii

    new URLRequest cant see my var in time

    jgsstrappy
      I am calling a function that sends some form data off. Depending on what form data they have pushed, I want to redirect them to different URLs. I get these URLs back in XML using the lastRequest method. I know they are there but when I run this script I get an error.

      submitBut( ):VOID
      {
      submitClient()<-- sends the form items to an .asp page and returns some XML data containing a URL
      var a:URLRequest= new URLRequest(submitClient.lastResult.data.items.url)
      navigateToURL(a ,"_self");
      }

      To me it looks as though the lastResult does not fill in time for the URL to have anything in it. I tried binding it to a text field and then sticking the text field in the URLRequest. That works IF I hit the button twice, meaning it works when the data was bound to the text field during the first submit. So the short of this question is, how do I get a page to quickly redirect to a URL that is a var coming back from the same GET request?

      Thanks for any help!
      Jon
        • 1. Re: new URLRequest cant see my var in time
          chris.huston.t10 Level 3
          You need to put your redirect code in a separate function. Set your submitClient function to call the redirect function when it gets the returned data. If you are submitting your request via AS, add an event listenter for "result". If you are calling an HTTPService defined in MXML, then just add: result="redirect_url()"

          private function submitClient():void {
          ...
          yourServiceCallHere.addEventListener("result", redirect_url);
          }

          private function redirect_url(evt:ResultEvent):void {
          var a:URLRequest= new URLRequest(submitClient.lastResult.data.items.url)
          navigateToURL(a ,"_self");
          }

          Vygo
          • 2. Re: new URLRequest cant see my var in time
            jgsstrappy Level 1
            Thanks for the quick post. I will give it a try. I have not worked with Listeners much. What do you mean when you say yourServiceCallHere on the listener? So in theory what i want to do is listen to submitClick and when it returns the goods then I tell it to run redirect_url and it goes. Is that correct? Do i have the theory right? I dont just want to copy and paste and not understand!

            Thanks again,
            Jon
            • 3. new URLRequest cant see my var in time
              chris.huston.t10 Level 3
              Yes, you have the right theory.
              Are you sending off the form data using HTTPService with something like :
              <mx:HTTPService
              id="submitClient"
              method="POST"
              result="redirect_url(event)"
              url="your.form.submit.script.here"
              useProxy="false"/>

              If you are, then you do not need to add a listener since the result="redirect_url(evt:ResultEvent)" takes care of this. So your redirect function would look like this:

              private function redirect_url(evt:ResultEvent):void {
              var a:URLRequest= new URLRequest(evt.result.data.items.url);
              navigateToURL(a ,"_self");
              }

              Also, make sure you have:
              import mx.rpc.events.ResultEvent;
              in your AS so you don't get an error

              Vygo
              • 4. Re: new URLRequest cant see my var in time
                ntsiii Level 3
                And to fill in a bit here, ALL data service requests in Flex are asynchronous. You MUST use events to process the result data.

                Also, avoid using lastResult like that. It is intended for binding expressions.

                Instead, pass the event into the handler function, and use event.result.

                Tracy