Search code examples
htmlapache-flexactionscriptmxml

How to access html request parameters in external mxml


From an HTML form, I wish to launch my existing working flex application passing in parameters E.G, login details.

Lets say I have a simple HTML page with no FLASH in it at all.

<h1>Test sending parameters</h1>
  <form name="login" action="http://example.com/myflexApp/index.html" 
                             target="_new" method="POST">
  username: <input type="text" name="username"  />
  password: <input type="text" name="password"  />
  <input type="submit" value="Submit" />
</form>

This will open up my application for the first time in a new browser window

My index.mxml which is compiled to create index.html looks like this (simplified):

 <?xml version="1.0" encoding="utf-8"?>
 <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
           xmlns:s="library://ns.adobe.com/flex/spark"
           ...
           creationComplete="init(event)" xmlns:omp="omp.*">
   <fx:Style source="defaults.css"/>
   <fx:Script>
    <![CDATA[
        import ...

        protected function init(event:FlexEvent):void
        {
          if (params[username] && params[password])//psuedo code
          {
             Alert.show(params[username],params[password]);//debugging
             //login(params[username],params[password]);//psuedo code to log in automatically
          }else{
           //existing code to show login form which works
          }
        }
    ]]>
   </fx:Script>
   <mx:ViewStack id="vs" width="100%" height="100%" ...>
   </mx:ViewStack>
 </s:Application>

So what should the html code look like and what is the corresponding actionscript code that goes into the init() function?

Or, at least what should I be googling for?

Note: Obviously it is not acceptable to have the parameters and values showing in the browser's URL address. Otherwise this would be easy.

Also note, the application is not embedded in the original HTML page, although 99.9% of search results I found on this topic gives an example of what to do if it was.

Ideally the original request would be a POST request but apparently flex can't handle post requests.

I've tried flashvars to no avail (although not entirely sure of the HTML format if it's not sending to an embedded SWF inside the HTML - and like I said, found 100's of examples if it was)

HTML:

<param name="flashvars" value="test='default text'" />

MXML:

if (FlexGlobals.topLevelApplication.parameters.hasOwnProperty("username"))
{
 Alert.show(FlexGlobals.topLevelApplication.parameters.username);
}

//Alert.show(LoaderInfo(this.root.loaderInfo).parameters.username);//also fails

So simple in most other languages I've worked with but having no luck with FLEX. I'm obviously missing something basic.


Solution

  • googling this i found the following example:
    http://help.adobe.com/en_US/flex/using/WS2db454920e96a9e51e63e3d11c0bf626ae-7feb.html

    there's is a code snippet that should trace all passed `flashVars``

    <?xml version="1.0" encoding="utf-8"?>
    <!-- wrapper/FlashVarTest.mxml -->
    <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
        xmlns:mx="library://ns.adobe.com/flex/mx" 
        xmlns:s="library://ns.adobe.com/flex/spark" 
        creationComplete="init()">
    
         <s:layout> 
            <s:HorizontalLayout/> 
         </s:layout>
    
         <fx:Script><![CDATA[
             import mx.core.FlexGlobals;
    
             private function init():void {
                  for (var i:String in FlexGlobals.topLevelApplication.parameters) {
                     ta1.text += i + ":" + FlexGlobals.topLevelApplication.parameters[i] + "\n";
                  }
              }
          ]]></fx:Script>
    
          <s:Label text="flashVars"/>
          <s:RichText id="ta1" width="300" height="200"/>
    
    </s:Application>
    

    maybe try this first and you'll see what parameters you're passing to your flex-app.

    your html should look something like this:

    <html>
    <head>
    <title>code/wrapper/SimplestFlashVarTestWrapper.html</title>
    <style>
        body {
            margin: 0px;
            overflow:hidden
        }
    </style>
    </head>
    <body scroll='no'>
    <table width='100%' height='100%' cellspacing='0' cellpadding='0'><tr><td valign='top'>
    
    <h1>Simplest FlashVarTest Wrapper</h1>
    
        <object id='mySwf' classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' height='100%' width='100%'>
            <param name='src' value='FlashVarTest.swf'/>
            <param name='flashVars' value='username=Nick&password=Danger'/>
            <embed name='mySwf' src='FlashVarTest.swf' height='100%' width='100%' flashVars='username=Nick&password=Danger'/>
        </object>
    
    </td></tr></table>
    </body>
    </html>
    

    you have to add the flashvars twice, not just once. and i don't really know if flashvars like the ' or the <space> you added in your example.

    one other thing: you don't want to POST your user's password as plaintext - use somekind of md5-hash to encode it.