URLRequest in AS3

I must say it’s kind of frustrating that AS3 isn’t that well documented yet, neither by Adobe nor the programming community. When searching for solutions almost everything you find is for AS2, and since it feels like half the language has been deprecated in the new version you end up spending much time just trying to find out how to do basic stuff.

For example, I’ve been spending some time today working out how to easiest serialize (if that was supported in AS3) arrays to send them to a php file to submit them to the database. For serializing I ended up using a basic for-loop and ‘.’ as divider, but you could also download the package provided by http://www.sephiroth.it for easy standard serialization (php style).

More frustrating was finding out the right way to send the data.
The AS2 way that is easily available online is to use the LoadVars object, something along the line of:

sender = new LoadVars();
sender.answers = serializedAn;
sender.timestamps = serializedTi;
sender.send("/URL/file.php", "_self", "POST");

The AS3 way that took me some time to find and get up working is:

var request:URLRequest = new URLRequest ("./URL/file.php");
request.method = URLRequestMethod.POST;
var variables:URLVariables = new URLVariables();
variables.answers = serializedAn;
variables.timestamps = serializedTi;
request.data = variables;
navigateToURL(request, '_self');

Now it works like a beauty.

Advertisement

One Response to URLRequest in AS3

  1. [...] URLRequest in AS3 August 2009 [...]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.