0
votes

I'm new to ActionScript and Flash environments (just started) and I'm trying to get data from a .php file. So I found this AjaxRequest class on the internet.

And I'm testing it with my Main actionscript class, which is in the same package with AjaxRequest.as

My function (constructor of HelloWorld.as) is:

// Create a TextField object and set its content to my php (AJAX) output
var t:TextField = new TextField( );
//Line 38 is this :
t.htmlText=new AjaxRequest("http://localhost/my_ajax_agent.php?action=flash");
addChild(t);

When I run the flash file that has main class as HelloWorld.as, I get the following error:

C:\path\to\HelloWorld.as, Line 38 1067: Implicit coercion of a value of type pakaj:AjaxRequest to an unrelated type String.

Edit: My question is this :

What should I do in order to get an AJAX call's result as a String?

Thanks for any help !

Note: My *ajax_agent.php* file only does this.

if(@$_GET['action']=="flash"){echo "Hello World";}
2

2 Answers

0
votes

In order to receive anything from that request you have to call AjaxRequest.send() first, and then you have to wait until that request will return data (query URLLoader.data AFTER it will receive its own Event.COMPLETE event). Anything that's involving external data sources is asynchronous, meaning the result isn't available immediately. Of course you cannot directly receive that request's result, so assigning new AjaxRequest() to a TextField is rightfully erroneous. The error says just that. Use this manual and this set of instructions to learn how to employ loaders and receive what did they load.

0
votes

Your code is wrong because you're trying to assign AjaxRequest to String (TextField's htmlText property is String-typed).

You have to write a link into your htmlText, using HTML "a href" tag.

If you want a request result, then you have to add event listener to request and execute it. When your request is comleted, the event will trigger, and you can retrieve String result data from it. So, put this data into your TextField's htmlText.