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??
1 2 3 4 | var lv:LoadVars = new LoadVars(); lv.username = "ruliman"; lv.password = "123456"; lv.sendAndLoad("somefile.php",lv,"POST"); |
nice isn’t??
in AS3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | 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
11 Comments
So how would I do the as3 equivelant to the ‘andLoad’ part of sendAndLoad?
I tried loader.addEventListener(Event.COMPLETE, dataIn);
Need to get data back from the DB once its made the request, but no luck.
Ahh nevermind I figured it out
yep, is :
loader.addEventListener(Event.COMPLETE, onComplete_handler);
function onComplete_handler(event:Event) {
var vars:URLVariables = new URLVariables(event.target.data);
trace(vars);
}
you just need to pass all variables in the echo separated by “&”
cheers.
I did something similar. I made a new loader rather than new variables.
loader.addEventListener(Event.COMPLETE, onDataIn);
function onDataIn(e:Event)
{
var loader:URLLoader = URLLoader(e.target);
var result = loader.data.result;
//result would be what gets echo’d out [ie. result=success
}
I’m pleased to know grupow has a labs blog, this “tip” solves me some questions, thanks. What about using navigateToURL(request)?
Hi Wences! thanks for your comment
as far as i know navigateToURL is like getURL in AS2 but you could send variables via navigateToURL something like:
var url:String = “http://www.grupow.com”;
var variables:URLVariables = new URLVariables();
variables.sessionId = new Date().getTime();
variables.userLabel = “ruliman”;
var request:URLRequest = new URLRequest(url);
request.data = variables;
try {
navigateToURL(request);
}
catch (e:Error) {
// handle error here
}
but the URLLoader is the fancy way =D
cheers
hi all,
how can i get more than 1 variable. here, name -> result and value->success, but i want to get more than 1 variable.
actually i want a ComplexObject in response but i dont think it can happen in AS3 so what i m thinking to do is evaluating all the data in the ComplexObject and then form a query string but before i strated to do so I must know do I really able to pass a query string with more than 1 variable or not?
thanks
hi Haris
umm for a ComplexObject you need to switch to AMF
you can use AMFPHP is very easy to use
for receiving more than one variable in the query string you just need to separate all variables by a “&” (you can see URLVariables for more info)
in the php the echo would be something like :
echo “result=success&users=user1,user2,user3&importanVar=10&”;
you can see that users variable the value is “user1,user2,user3″;
back in AS3 you can split that string and access like an array
And how do I do to get the response in case the PHP file generates some content?
you mean the PHP is returning some variables?¿
Thank you very much. Very helpful!!!