Rich Rodecker’s blog on flash, flex, actionscript, javascript, and php, with a dash of randomness
Error #2101: The String passed to URLVariables.decode() must be a URL-encoded query string containing name/value pairs
I ran into this issue today trying to load some simple data from a php script. I was positive I was only sending name/value pairs and nothing else.  I had set the loader.dataFormat property to URLLoaderDataFormat.VARIABLES, and using Charles I can see that my data was arriving fine.
The solution in my case was simple:  I was adding a leading ‘&’ in the variables string, which URLLoader doesn’t appreciate. So instead of this, which was a habit picked up with AS 2:
echo "&result=success"
You just need to do:
echo "result=success"
You still need the ampersand to separate multiple name/value pairs though.
ÂÂ
| Print article | This entry was posted by rich on November 27, 2007 at 1:23 pm, and is filed under flash. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |
about 5 years ago
yah… havin’ this error a month ago
If you don’t know what it can drive you crazy. (happen to me:)
about 5 years ago
So you didn’t find a solution for AS 3? Because I kind of have the same problem…the data from the php file is loading, but I can’t read out the variables properly respectively I can’t read them out at all…instead I get the same error as above when starting my swf online…it’s kind of driving me crazy right now and I would really appreciate some help in this situation. unfortunately (or not) I’m using as 3.0 and I wanna stick with it solving this problem.
Maria
about 5 years ago
That solution was for AS 3. Make sure you are not sending back your variables from php starting with a ‘&’ sign, and make sure you have
myLoader.dataFormat = URLLoaderDataFormat.VARIABLESabout 5 years ago
Yes! Thanks! it was very helpfull to me.
about 5 years ago
Thanks – that fixed my problem…
about 5 years ago
thanks & thanks
…
i will get old early .. as3 makes me crazy
about 5 years ago
Many thanks…..it worksss hahahah
about 5 years ago
Thanks, very helpful. My situation was slightly different…
In my case the error was because the php page wasn’t returning anything at all, so I threw in an echo(“err=0″); to resolve it.
about 4 years ago
my question now is… does the as3 way work for as2 as well
about 4 years ago
Thank you…
Mike^ AS2 does not have this problem, just AS3.
That is just ridiculous BTW. How can the AS2 version be more robust than the AS3 version. It is such a simple standard, how can they fail so badly at re-implementing something they were already doing right?!
If it doesn’t allow multiple & between name/value pairs I will be SO so ticked.
about 4 years ago
Okay. Grunt work to the rescue! I was so pissed I re-invented the wheel… Well kinda’.
This function will strip out all extra “&” so that AS3 URLVariables.decode() won’t complain:
static public function strip(urlEncoding:String):String{
var x=-1;
while((x = urlEncoding.indexOf(“&”, x+1))!= -1){
var y=x;
while(urlEncoding.charAt(++y)==”&”);
urlEncoding = urlEncoding.substring(0,x+1)+urlEncoding.substring(y);
}
if(urlEncoding.charAt(0)==”&”)urlEncoding = urlEncoding.substring(1);
if(urlEncoding.charAt(urlEncoding.length-1)==”&”)urlEncoding = urlEncoding.substring(0,urlEncoding.length-1);
return urlEncoding;
}
about 4 years ago
Thanks!
Helped so much!
about 4 years ago
Hi Rich
I couldnt solve proble with this.
myLoader.dataFormat = URLLoaderDataFormat.VARIABLES
could you plz explain in detail ?.
Thanks in Advance.
about 4 years ago
nice blog guys! it helps a lot! Thanks!
Cheers!
about 4 years ago
hi, all
i want to ask one thing;
what if from PHP script, I want to send a complex Object in response to AS3? Its not working either. can someone shed light upon this?
thanks
about 4 years ago
Dude you are a life saver. Been searching everywhere for this though I still have a long way to go, you got me started.
Its funny how all the AS3/Php/MySQL tutorials online are very basic and never really deal with what happens ‘after’ you successfully query a database and the query returns a bunch of variables, how do you handle these individual variables in flash?
Maybe something like this ….
function emailOnComplete(e:Event):void {
var msg;
var vss:URLVariables;
var loader1:URLLoader = URLLoader(e.target);
loader1.dataFormat = URLLoaderDataFormat.VARIABLES;
vss = new URLVariables(loader1.data);
msg = vss.msg;
username.text = msg;
}
about 4 years ago
So the running problem the *entire* time was to do with the server’s response to Flash? Jesus, who the hell would have thought? Thanks for pointing this out mate, you saved me a serious headache… I sensed another 3-hour long “searching and pulling hair session” coming on.
I was mislead because a prebuilt PHP script I was given to look through had used the incorrectly encoded string “&variable=value&”, and since that was a script with a finished result, I assumed it’d been tested as working. I guess not then. xD
about 4 years ago
query string shouldn’t have a leading “&”. This won’t work with js, or PHP to send the variable. The problem isn’t with AS3 it’s with you for building bad query string. AS2 should not have accepted these strings either.
about 4 years ago
Paul – I’m not sure if your comment was targeted towards my post or somebody else’s comment, but I clearly state it my post that it was “a habit picked up with AS 2″. Not sure if you have done any AS 2 development, but there were times when it was easier/more convenient to pull stuff like that.
about 4 years ago
Thanks
about 4 years ago
I’m affraid this is oversimplified..
I get the same error while trying to get an url that is invalid (random domain with random tld, like for example http://thisdoesntexistblahblazzz.xyz/?test1=1&test2=2)
The problem for me however is that of finding the right catch and/or event handler to listen to.
I based my code on the adobe example: http://help.adobe.com/en_US/AS3LCR/Flash_10.0/flash/net/URLLoader.html#includeExamplesSummary
However… even with all those handlers in place i still cant catch the “#2101″ error in my code! i still only see the message in the console at runtime.
*angry*
Well commenting sortof helped med vent my frustration. Thanks
about 4 years ago
I have the exact same problem as Lennart here above me.
I get #2101 when trying to retrieve a PHP page on my webserver that is down. The PHP works flawlessly when the server is up, then when I turn it off, I just get the funny #2101 error. I even tried adding an IOErrorEvent-handler, which doesn’t help at all…
For some reason event listener tells me that the fetch was completed, and did not throw any error at all, until I try to manipulate the data in the eventComplete-function.
Weird.
about 3 years ago
Wow, big thanks.
You just saved me a whole lot of hassle.
about 3 years ago
I had this same the same reported error but the name/value pairs was fine.
It turned out that my URLRequest String needed an question mark on the end like so:
var requester:URLRequest = new URLRequest("http://localhost/file.php?");
Seems a bit odd that this occurs but there u go.
about 3 years ago
@joshua – haven’t heard that one before…I gotta say I think there may have been something else going on there. The question mark just signifies the beginning of a query string, and that shouldn’t matter as to the result being sent back from the server.
about 3 years ago
@Joshua Langley
yes you must put ? in the end of your link
var phpfile:String = “http://localhost/f/m.php?”;
it work with me
thanks
about 3 years ago
I got same error, removed all & from AS 3.0 script added ? to end of php script link, but dont fully understand “myLoader.dataFormat = URLLoaderDataFormat.VARIABLES”.
please help.
Thank you in Advance
about 3 years ago
I still have the same problem. I have copied all the scripts of this page and still get the same error.
I only call my php file with one simple variable and all it does is send a very simple echo back.
But no luck!
AS3 is so badly documented it’s terrible! Just a simple working example from adobe would be enough, but no. Alas.
about 3 years ago
So i read trough all the coments and found my solution with the ? at the end of the http string.
So my string looked like this
http://localhost/airtools/network.php?type=serverstatus&arg1=10.0.0.10
and that failed, but adding a ? to the end fixed it so it turned out like this:
http://localhost/airtools/network.php?type=serverstatus&arg1=10.0.0.10?
That did it all. the wtf ? at the end. Thought i should clarify it because i dident get it at first. and finally the request line from as3
var request:URLRequest = new URLRequest(“http://localhost/zerosubnet/airtools/network.php?type=serverstatus&arg1=10.0.0.10?”);
Thanks for all the great answers, gotta go and get some more trouble:=)
about 3 years ago
Here’s my AS3 code. It shows the PHP file that I’m using. The PHP file works fine. No problem. It calls some variables from a MySQL database and displays them as a &-separated string of variable pairs.
When I publish the ActionSript code and run the html on my localhost, it also works great, IF I don’t try to display the database variables. But when I do call the MySQL data, I get an error saying that my textfields can’t display null values. In other words, the dB data isn’t there. But, to repeat, running the PHP file directly does show the dB values.
Is Flash running too quickly and not waiting for MySQL to respond?
Example();
function Example() {
// Create the URLLoader instance to be able to load data
var loader:URLLoader = new URLLoader();
// Define the event handler to be invoked when the load completes
loader.addEventListener( Event.COMPLETE, handleComplete );
// Configure the loader to load URL-encoded variables, not text
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
// Attempt to load some data
loader.load( new URLRequest( “variable.php” ) );
}
/*
-Here’s the PHP file I used: variable.php
**Note: It will also work if the order of the echos is at the end and reversed,
as long as there is an & in between them!
-And here’s the output of the PHP file:
someText=Wow, Rick! Its working!&otherText=Crap, Rick! Its not working!
-The Flash output (running the .html file on http://localhost) is:
Wow, Rick! Its working!
Crap, Rick! Its not working!
*/
function handleComplete( event:Event ):void {
// Cast the event target as a URLLoader instance because that is
// what generated the event.
var loader:URLLoader = URLLoader( event.target );
var tmp1:TextField = new TextField();
var tmp2:TextField = new TextField();
var tmp3:TextField = new TextField();
var tmp4:TextField = new TextField();
addChild( tmp1 );
addChild( tmp2 );
addChild( tmp3 );
addChild( tmp4 );
tmp1.text = “abc”;
tmp2.text = “def”;
tmp3.text = “ghi”;
tmp4.text = “jkl”;
tmp1.width = 150;
tmp2.width = 150;
tmp3.width = 150;
tmp4.width = 150;
tmp1.y = 0;
tmp1.x = 20;
tmp2.y = 20;
tmp3.y = 40;
tmp3.x = 20;
tmp4.y = 60;
var PHPvars:URLVariables;
PHPvars = new URLVariables( loader.data );
trace( “someText = ” + PHPvars.someText );
trace( “otherText = ” + PHPvars.otherText );
tmp1.text = PHPvars.someText; // I tested this and it worked great.
tmp2.text = PHPvars.count;
// up to this line, EVERYTHING WORKS GREAT!!!!
tmp3.text = PHPvars.Ttl;
tmp4.text = PHPvars.Cmt;
}
THANKS TO ANYONE WHO CAN HELP ME.
about 3 years ago
Huh. The PHP code didn’t show up in that comment.
Here it is:
$connect = mysql_connect(“localhost”, “root”, “”);
mysql_select_db(“anastasio”, $connect);
$result = mysql_query(“SELECT Title, Comments, Image FROM titles”);
$count = 0;
while($row=mysql_fetch_array($result)){
$Ttl=$row['Title'];
$Cmt=$row['Comments'];
$Img=$row['Image'];
echo “Title$count=$Ttl&Comments$count=$Cmt&Image$count=$Img&”;
$count++;
}
echo “count=$count&”;
$someText = ‘Wow, Rick! Its working!’;
$otherText = ‘Hey, Rick, is it working?’;
echo “otherText=$otherText”;
echo “&someText=$someText”;
about 3 years ago
many thanx!
about 3 years ago
Hi guys,
Yes, I’ve also been pulling my hair out over this #2101 error in Flash AS3 when talking to a PHP file that cant be found. This often happens when the server is down or when the users internet connection is dead or too busy.
Here are my findings so far. I stumbled upon them after seeing exactly what Flash was actually receiving from the ‘failed’ load. Maybe they will help others who are also stuck and on the verge of taking a hammer to the screen.
First off: I’m fairly convinced its a bug in Flash Player 10.
If I set the return format as ‘URLLoaderDataFormat.VARIABLES’, FP10 doesn’t throw the expected IOError at all but fails with a #2101 error.
The moment I change the return format to ‘URLLoaderDataFormat.TEXT’, the correct IOError is thrown and everything works fine.
So I believe that the problem is that FP10 is throwing the correct IOError for the TEXT format but NOT for the VARIABLES format.
This also explains why some people are complaining that their Flash forms worked fine in FP9, but now have issues in FP10.
I believe that is the root of the problem, and it
makes sense and leads to the next problem that many of us are experiencing.
When a page cannot be found on the server, an error page is sent instead. When an error page is sent, Flash is supposed to throw an IOError and catch it before the ‘complete’ handler is called.
But for some reason Flash doesnt throw an IOError when the return format is set as VARIABLES (a possible issue with FP10), which means whatever the server returns is sent straight to the ‘complete’ handler. So technically speaking, according to Flash, the file has downloaded successfully with no problems.
So the ‘complete’ function is called, but because the returned data is an HTML formatted error page, and NOT your expected variables, Flash naturally throws the #2101 error because the URLVariables object cant decode the HTML page into variable pairs.
The moment you change the return format to TEXT, everything works fine because the HTML formatted error page is processed just fine as TEXT.
While changing the return format to TEXT may work well because it throws the correct IOError, its still very annoying and inconvenient because most of us are wanting to use the VARIABLES format for a reason!
And Flash is SUPPOSED to catch the error page and throw an IOError, but ALAS, NOT for VARIABLES!!!
We expected it to work as advertised! Also, changing the return format to TEXT is inefficient because it requires writing extra steps to then convert the loaded TEXT to VARIABLE pairs.
So for those who are also struggling with this, your AS3 and PHP are probable fine. The problem is that Flash is not throwing an IOError for the VARIABLES format. This results in the returned HTML formatted error page from the server being sent straight through to the ‘complete’ handler and throwing everything into chaos!
Untill Adobe sorts it out, it seems the only workaround is to have the return format as TEXT and then convert the TEXT to VARIABLES afterward.
If anyone else has any suggestions or discoveries on this, I would love to hear from you!
about 3 years ago
thanks Jared… you are so f*!”$% right, just solved my problem by changing VARIABLES to TEXT
THANKS AGAIN… and for this time lets blame Adobe for all my/our headaches
about 3 years ago
Spot-on.
about 3 years ago
Ive read all of the tips, above and tried them, but still i can´t solve me problem. And the worst thing is, and for sure thats the thing giving this error, is that each time i update the inputfield, my databasefield in WAMP goes blank.
Why?
I already lost days investigating, stupid as3 programming.
If someone would like to help, PLEASE!
about 3 years ago
First time working with URLLoader I stumbled upon the above mentioned error #2101.
At first, my text data file looked like:
&var1=value1&
&var2=value2&
…
That’s how I’ve done it all my AS 2 life.
What now (only) works is:
var1=value1&var2=value2
So, no leading and no ending ampersand.
about 3 years ago
And here’s the simple AS 3 code which works for me:
var url:String=”data.txt”;
var request:URLRequest = new URLRequest(url);
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.VARIABLES
loader.addEventListener(Event.COMPLETE, loadComplete);
loader.load(request);
function loadComplete(e:Event):void {
trace(e.target.data);
}
about 2 years ago
I came across this problem many times and usually its the;
myLoader.dataFormat = URLLoaderDataFormat.VARIABLES ;
line which i missed out.
try to remove this line if you have it:
requset.method = URLRequestMethod.POST;
and finally make sure you are receiving a response through a variable;
theStatus=okay
then that should do the trick.
about 2 years ago
thanks very much for this post, very useful!!
about 2 years ago
POSSIBLE SOLUTION:
I was getting this error only for player version 10.0.2.54 and this saved me:
http://www.actionscript.org/forums/showpost.php3?p=902826&postcount=12
about 2 years ago
If you have a trailing & at the end of the URL response, then it returns the same error.
about 2 years ago
Weird solution ( to pass the vars with URLLoaderDataFormat.VARIABLES
) :
I was doing some test this afternoon after i had the same error like you guys and this is how i fix it in my case:
||||||||||||||
| The code : |
||||||||||||||
————————————————————————————————————-
As3
—
var stupidBugA:String;
var stupidBugB:String;
var myRequest:URLRequest = new URLRequest(“http://yoursite/your.php”);
var myLoader:URLLoader = new URLLoader();
myRequest.method = URLRequestMethod.GET;
myLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
myLoader.addEventListener(Event.COMPLETE, myLoaderFinished);
myLoader.load(myRequest);
function myLoaderFinished(e:Event):void{
stupidBugA = e.target.data.testa;
stupidBugB = e.target.data.testb;
myLoader.removeEventListener(Event.COMPLETE, myLoaderFinished);
}
————————————————————————————————————–
————————————————————————————————————–
Php not working for Flash
—
————————————————————————————————————–
————————————————————————————————————–
Php fixed for Flash
—
————————————————————————————————————–
||||||||||||||||||||
| The explanation: |
||||||||||||||||||||
————————————————————————————————————–
I noticed when i was debugging the swf file that in the moment when the loading from the server was
finished and when the result (testa & testb) were about to be read in the function myLoaderFinished that the
first result (testa) could not be assigned to the var stupidBugA even if it was shown that it contain some information inside
the Variables/e/currentTarget/data.
After some useless tests i noticed that the name of the result (testa) inside the Variables/e/currentTarget/data is not written
properly. It looked like there was some space before the name of the testa.
First i thought that is just the question of formatting and nothing else but i decided to try to add some fictive var inside of the
php and that is it. That is how it worked for the Flash once sent from my server.
The fictive var ( fix ) is send with nothing in it to the Flash and two other important vars are sent after it with echo(“fix=$fix&testa=$testa&testb=$testb”);
The fictive var ( fix ) inside Variables/e/currentTarget/data is now having this weird space before and is not readable and two others ( which i need anyway )
are readable from Flash.
When i would change the position of the var inside the php echo(“fix=$fix&testa=$testa&testb=$testb”); and write for example echo(“testa=$testa&fix=$fix&testb=$testb”);
i would again have the problem with the testa and so on …..
I guess that you just need to sacrifice the first var sent to Flash and the others will work.
Does this space before the first var means that there is some problem with that var for the Flash or the Flash is translating the first var name as ” fix” instead of “fix”, i do not know and i do not care. It works and i do not want to bother any more with it.
Like i said in the beginning, weird solution, but at least i can pass the vars with URLLoaderDataFormat.VARIABLES
I hope that this will help someone.
————————————————————————————————————-
about 2 years ago
Php not working for Flash
—
( $testa = “testing1″; $testb = “testing2″; echo(“testa=$testa&testb=$testb”); )
Php fixed for Flash
—
( $testa = “testing1″; $testb = “testing2″; $fix = “”; // Serves only to repair the stupid bug in my case echo(“fix=$fix&testa=$testa&testb=$testb”); )
about 2 years ago
thanks so much for this! Worked a treat
about 1 year ago
Hi,
I was playing around with the URLVariables and got this error:
Error #2101: The String passed to URLVariables.decode() must be a URL-encoded query string containing name/value pairs.
It is not allowed to pass an empty string to the constructor of URLVariables, is must be a valid encoded URL, or a null-object.
Maybe this will help someone else.
Aye
about 1 year ago
Hi,
Thanks, this post has been of great help.
Just wanted to let you know that I found a mistake (I think) on the code shared by CobaltBlue (thanks for`sharing the code), post 11:
I had to modify the following line:
From:
if(urlEncoding.charAt(urlEncoding.length-1)==”&”)urlEncoding = urlEncoding.substring(0,urlEncoding.length-1);
To:
if(urlEncoding.charAt(urlEncoding.length-2)==”&”)urlEncoding = urlEncoding.substring(0,urlEncoding.length-2);
Note: I changed “length-1″ to “…length-2″.
I had to do that modification otherwise it won´t work for me.
So now, I use …URLLoaderDataFormat.TEXT and then use the strip function to read the vars:
var vars:URLVariables = new URLVariables(strip(e.target.data));
Working great!
Just wanted to share this in case anyone had the same problem.
Best regards.
Rafael Laudo