Sending Variables in AS3 A.K.A. sendAndLoad in AS2

| Comments

in AS2 sending and receiving variables was easy, you could do it even with your eyes closed =D but in AS3 is another story

remember AS2??

var lv:LoadVars = new LoadVars();
lv.username = "ruliman";
lv.password = "123456";
lv.sendAndLoad("somefile.php",lv,"POST");

nice isn’t??

in AS3

var variables:URLVariables = new URLVariables();
variables.username = "ruliman";
variables.password= "123456";
//
trace(variables.toString());
var request:URLRequest = new URLRequest("somefile.php");
request.method = URLRequestMethod.POST;
request.data = variables;

var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.VARIABLES;

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

until here you can say “well it isn’t too bad just a bunch of classes, but is better organized =) i love AS3 ”

but……..when you compile Flash tell you this!

Error: Error #2101: The String passed to URLVariables.decode() must be a URL-encoded query string containing name/value pairs. at Error$/throwError() at flash.net::URLVariables/decode() at flash.net::URLVariables$iinit() at flash.net::URLLoader/flash.net:URLLoader::onComplete()

MUAHAHAAA!!!

the PHP code is simple

1
2
3
4
<?
$user= $_POST["username "];
$pass= $_POST["password"];
?>

so…………where the error is coming from?? i google it and found this answer, is very simple: my php wasn’t returning something so, put an echo throwing some variable something like:

1
2
3
4
5
6
<?
$user= $_POST["username "];
$pass= $_POST["password"];

echo "result=success";
?>

and that’s it everything compiles normal, now i can go back to dinner =D

Comments