views:

254

answers:

2

Hi,

I am trying to read in a string from a C# Response.Write("string example") into ActionScript for a swf file

My actionscript code looks like this

   var requestVars:URLVariables = new URLVariables();
   requestVars.ornTest = "test";
   var request:URLRequest = new URLRequest();
   request.url = "http://localhost/apps/game/tree/DesignFlash.aspx";
   request.method = URLRequestMethod.GET;
   request.data = requestVars;

   var loader:URLLoader = new URLLoader();
   loader.dataFormat = URLLoaderDataFormat.TEXT;
   loader.addEventListener(Event.COMPLETE, loaderCompleteHandler);
   loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
   loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
   loader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);

   try
   {
  loader.load(request);
   }
   catch (error:Error)
   {
  trace("Unable to load URL");
   }

           private function loaderCompleteHandler(event:Event):void
 {
    var variables:URLVariables = new URLVariables( event.target.data );
    if(variables.success)
    {
     var ornArray = deserializeString("read string from C# here");
     for(var i:int=0;i<ornArray.length;i+=3)
     {
      addOrnamentProperty(ornArray[i],ornArray[i+1],ornArray[i+2]);
     }
     addOrnamentsFromArrayList();
    }
   }  

 private function httpStatusHandler (event:Event):void
 {
    //trace("httpStatusHandler:" + e);
 }

 private function securityErrorHandler (event:Event):void
 {
    trace("securityErrorHandler:" + event);
 }

 private function ioErrorHandler(event:Event):void
 {
  trace("ioErrorHandler: " + event);
 }

...And My C# looks like this

protected void Page_Load(object sender, EventArgs e)
    {

            var test  = Request["ornProperties"];

            if (!String.IsNullOrEmpty(Request.Params["ornTest"]))
            {
                string paramVars = Request.Params["ornTest"];

                Response.Write("this string");
            }

    }

How do I write "this string" from the C# into the ActionScript of my SWF file at the point that says "read C# string here" within the loaderCompleteHandler?

Thanks

A: 

First do a simple trace to ensure you are getting a response from the server:

private function loaderCompleteHandler(event:Event):void
{
    trace("Received server response:", event.target.data);
}

Your response should be in the data field of the returned value. If you want to return key-value pairs (which is what your current ActionScript code expects) you need to format your return text to support it.

e.g.

Response.Write("someKey=someValue&anotherKey=anotherValue");

I don't know C# well enough to know if Response will url encode the data but the data should be url encoded. If you send key value pairs like so then you can use URLVariables as in your example to parse it into a more easily usable form.

e.g.

private function loaderCompleteHandler(event:Event):void
{
    var loader:URLLoader = URLLoader(event.target);
    var variables:URLVariables = URLVariables(loader.data)

    trace("Some Key:", variables.someKey);
    trace("Another Key:", variables.anotherKey);
}


Now I am no huge fan of XML but Flash works pretty well with that format instead. If you return xml in your response:

Response.Write("<root><blah>foo</blah></root>");

You can do:

private function loaderCompleteHandler(event:Event):void
{
    var loader:URLLoader = URLLoader(event.target);
    var xml:XML = new XML(loader.data);
}

then you can use E4X to manipulate the data. The The E4X approach to XML processing is really quite nice. I use it for anything less complex than full-on Flash remoting using AMF.

If you are going to be sending a lot of data back and forth between the server and Flash you should look into Remoting and AMF.

James Fassett
+1  A: 

You're very close, actually...

Be sure to clear out your .aspx file (except the Page element at the top) and call Response.Clear() before you Response.Write out your text. Otherwise you'll be sending html, head and body tags along with your text.

Then it's just a matter of looking at the loader.data in your load complete handler in Actionscript:

C#:

protected void Page_Load(object sender, EventArgs e)
{
   var test  = Request["ornProperties"];
   if (!String.IsNullOrEmpty(Request.Params["ornTest"]))
   {
       string paramVars = Request.Params["ornTest"];
       Response.Clear(); //just to make sure you're not sending anything else.
       Response.Write("this string");
   }
}

Actionscript:

    private function loaderCompleteHandler(event:Event):void
    {
              var variables:URLVariables = new URLVariables( event.target.data );
              if(variables.success)
              {
                      var ornArray = deserializeString(loader.data); //just look at loader.data here.
                                    for(var i:int=0;i<ornArray.length;i+=3)
                                    {
                                            addOrnamentProperty(ornArray[i],ornArray[i+1],ornArray[i+2]);
                                    }
                                    addOrnamentsFromArrayList();
              }
    }
blesh
This is exactly what I needed to know. loader.data was the key. Thanks a million
Adrian Adkison