AS3 receives data from PHP

I can’t accept Variable data, I seem to be doing everything right, referring to Adobe AS3 directories, I think this is a problem with PHP, but I'm not sure.

PHP - this file is in the same directory as the AS3 file

<?php $testData = 4; echo "testData=" . $testData ?> 

AS3

 public function URLLoaderDataFormatExample() { var request:URLRequest = new URLRequest("PHPTest.php"); var variables:URLLoader = new URLLoader(); variables.dataFormat = URLLoaderDataFormat.VARIABLES; variables.addEventListener(Event.COMPLETE, completeHandler); try { variables.load(request); } catch (error:Error) { trace("Unable to load URL: " + error); } } private function completeHandler(event:Event):void { var loader:URLLoader = URLLoader(event.target); trace(event.target.data.testData) } 

The trace statement should print 4. However, it just prints undefined.

+4
source share
3 answers

If I remember correctly, I also have to deal with this problem myself.

What I discovered is that sometimes (I didn’t find the actual reason) the first value that will be sent to PHP will be canceled or not even sent at all.

I used to add an unimportant value at the beginning of the echo, for example:

 echo 'unimportantvar=nothing&myXML='.$xml; 

It always seemed that this problem was being solved.

+1
source

Something like this should do it.

 private function completeHandler(event:Event):void{ var loader:URLLoader = URLLoader(event.target); var urlVars:URLVariables = new URLVariables(loader.data); trace(urlVars.testData) } 
+1
source

ActionScript code looks fine.

Flash is really sensitive to spaces before / after variables. Note that if you have a new line after ?> Or before <?php , it will be passed as part of the response.

To make sure PHP is a problem, replace the .php file with a simple .txt, just text testData=4 , with no extra lines or spaces, and try downloading this.

If this is loaded correctly, the problem is with the PHP file - go to PHPTest.php in the browser and check if something is added.

+1
source

Source: https://habr.com/ru/post/1439228/


All Articles