AS3 send flashvar to self on navigateToURL - actionscript-3

This is how i'm handling html flash vars:
var flashVars:Object = new Object();
flashVars = this.loaderInfo.parameters;
for (var item:String in flashVars)
{
switch (item)
{
case "phoneNumber" :
phoneNum = String(flashVars[item]);
break;
}
}
A little verbose for only one var, but i like to be flexible to change later on in the project..
anyways, it was more efficient to compose a quick reset function that simply reloads the swf than it would be to compose a matrix of properties for objects that i'm manipulating within the swf; thus, I'm using the following code:
function reset(){
var url = stage.loaderInfo.url;
var request = new URLRequest(url);
navigateToURL(request,"_level0");
}
What I need to do is pass the flashvars from the flashVars object to the request var and i think having a fever is making me over think this... anyone done this before?

You can just pass the flashvars data object to the data property of your URLRequest:
var url = stage.loaderInfo.url;
var request = new URLRequest(url);
request.data = stage.loaderInfo.parameters
navigateToURL(request,"_level0");

Related

getting POST using URLRequest + if statement syntax issue

I'm trying to get the reply from the address specified in var url:String, which is either 0 or 1. If the reply is 1 then it must set currentState = CallFailed (as seen below). The client compiles without error (using Adobe Flash Builder 4.6) and seems to successfully reach var url:String, but doesn't seem to be getting the response\and or my if statement is incorrect.
Actionscript:
// check to see if block.php replies 0 or 1
var url:String = "https://domain.com/block.php?postid=" + calleeInput.text + "";
var request:URLRequest = new URLRequest(url);
var variables:URLVariables = new URLVariables();
request.data = variables;
request.method = URLRequestMethod.POST;
navigateToURL(request);
if (request.data == 1)
{
// if reply is 1 then cancel the call
currentState = CallFailed;
return;
}
PHP:
PHP will echo 0 or 1 when block.php is loaded. It's not encoded in any format such as JSON\AJAX.
It seems you want data from the server. Perhaps the URLLoader class would be better?
var url:String = "https://domain.com/block.php?postid=" + calleeInput.text + "";
var request:URLRequest = new URLRequest(url);
var variables:URLVariables = new URLVariables();
request.data = variables;
request.method = URLRequestMethod.POST;
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.TEXT;
loader.addEventListener( Event.COMPLETE,
function( e:Event ) : void
{
// your response data will be here
// you'll have to verify the format
trace( e.target.data );
}
)
loader.load( request );
Put a breakpoint at the trace statement and check out the contents of e.target.data, and go from there
The purpose of navigateToURL() is to open the webbrowser, as stated in its documentation:
Opens or replaces a window in the application that contains the Flash Player container (usually a browser). In Adobe AIR, the function opens a URL in the default system web browser
In order to perform an request (without opening a browser, just the HTTP communication) you should use URLLoader.
The URLLoader class downloads data from a URL as text, binary data, or URL-encoded variables.
On a related note: your logic is not valid. The call to a server is asynchronous. You have to wait for the response to be returned before reasoning about the result.
The URLLoader class dispatches a number of Events that help you decide when the result of a request is returned or if there's a problem with it.

Running-order of nested function-calls delayed/altered due to URLloader in AS3?

First of all: English is not my first language. ;-)
I am compiling the following code:
var sqldata:String;
function sql(saveorload,sqlstring) {
var sqlloader = new URLLoader();
var sqlrequest = new URLRequest("http://***/sql.php");
sqlrequest.method = URLRequestMethod.POST;
sqlloader.addEventListener(Event.COMPLETE, sqldonetrace);
var variables:URLVariables = new URLVariables();
variables.sqlm = saveorload;
variables.sqlq = sqlstring;
sqlrequest.data = variables;
sqlloader.load(sqlrequest);
}
function sqldonetrace(e:Event) {
sqldata = e.target.data;
}
sql("1","SELECT * FROM songs WHERE `flag2` LIKE '0'");
trace (sqldata);
So, here comes the problem:
"sqldata" is traced as "null". AS3 seems to run "sql", then "trace" and then "sqldone", but i would need sql -> sqldone -> trace...
I can't put the trace-command in the sqldone-function because it is stored as *.as and loaded at different points in my .swf and not always followed by only a trace-command.
Any Ideas/hints/flaws in script?
Actionscript is, by design, an event driven language. It also guarantees that the code executes in a single thread i.e. if a function call starts, no other(new call) actionscript code would execute until the first call finishes.
That being said, what you should have is to pass a complete handler to sql(...).
function sql(saveorload:String, sqlstring:String, completeHandler:Function):void
{
var sqlloader = new URLLoader();
var sqlrequest = new URLRequest("http://***/sql.php");
sqlrequest.method = URLRequestMethod.POST;
sqlloader.addEventListener(Event.COMPLETE, completeHandler);//tell urlloader to use the complete handler passed in parameters
var variables:URLVariables = new URLVariables();
variables.sqlm = saveorload;
variables.sqlq = sqlstring;
sqlrequest.data = variables;
sqlloader.load(sqlrequest);
}
And then use it from some other place like:
sql("1","SELECT * FROM songs WHERE `flag2` LIKE '0'", sqldonetrace);
function sqldonetrace(e:Event)
{
var sqldata:String = e.target.data;
trace (sqldata);
}
Also, I think you should check for error events from urlloader. Pass another parameter to sql as errorHandler

howto atach properties to dynamic created moviclips

Hi I try to get acces a property by click on a dynamic created Moviclip.
function finishLoading(evt : Event):void {
// Handle XML Settings
XML.ignoreComments=true;
XML.ignoreWhitespace=true;
// Attach XML Data into XML Var
fXML=new XML(evt.target.data);
// Iterate XML response and build Preview List
for (var i:Number=0; i<fXML.mov.length(); i++) {
var sTmpTitle=fXML.mov[i].mov_title.text();
var sTmpSrc=fXML.mov[i].mov_src.text();
var sTmpThumb=fXML.mov[i].mov_thumb.text();
var sTmpOrder=parseInt(fXML.mov[i].mov_list_order.text());
var iPosY:Number = (sTmpOrder!=1)?(sTmpOrder-1)*105:0;
var sTmpLoader:Loader = new Loader();
sTmpLoader.load(new URLRequest(sTmpThumb));
var oTmpMc:MovieClip = new MovieClip();
oTmpMc.addChild(sTmpLoader);
oTmpMc.y=iPosY;
oTmpMc.x=0;
oTmpMc.mov_src = sTmpSrc;
oTmpMc.addEventListener(MouseEvent.CLICK, function()
{
trace(this.mov_src);
});
mc_slider.addChild(oTmpMc);
}
}
Creating the MOVIECLPIP and handle event is working well but I dont know hw I can get access on the property mov_src by click on the clip.
What´s to do to get this working.
Thank
Ben
First of all, you shouldn't add dynamic properties to movieclips, it's a bad habit from AS2. You should extend the MovieClip class and add the properties you want. Anyway, you can do what you want to do by externalizing your callback :
...
oTmpMc.addEventListener(MouseEvent.CLICK, onClipClicked);
mc_slider.addChild(oTmpMc);
}
private function onClipClicked(e:Event):void
{
var clip:MovieClip = e.currentTarget as MovieClip;
trace(clip.mov_src);
}

How do you read and write http headers in ActionScript?

Is it even possible to both read and write http headers in actionscript?
Hmm... ok setting them doesn't seem to be a problem.
Pretty straight forward, you just add and array of URLRequestHeader objects to the URLRequest object.
var req:URLRequest = new URLRequest();
var someheader:URLRequestHeader = new URLRequestHeader( "Connection", "OK" );
req.requestHeaders = [someheader];
However, you can't read them in the regular FlashPlayer. This can be done in AIR and Flash Lite on using the requestHeader property on the HTTPStatusEvent.
var loader:URLLoader = new URLLoader();
loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, onStatus);
private function onStatus( e:HTTPStatusEvent ) : void
{
var headers:Array = e.requestHeaders; // only available in FlashLite and AIR
}
This is kinda weird.

how to read the data from the JSP in a MXML through actionscript?

We are collecting the data at run time in Collection object in JSP page. We want to read this data from the JSP in a MXML through actionscript.
pls share the sample if you have.
thanks,
Sudarshan
function loadData():void
{
var ldr:URLLoader = new URLLoader();
var request:URLRequest = new URLRequest("page.jsp");
ldr.addEventListener(Event.COMPLETE, onLoad);
ldr.load(request);
}
private function onLoad(e:Event):void
{
var ldr:URLLoader = URLLoader(e.target);
trace(ldr.data);//traces the loaded string
//if the data is xml
/*
var myxml:XML = new XML(ldr.data);
trace(myxml.toXMLString());
*/
//update: answer to the comment:
//If the input string just lacks a root tag from being valid xml,
//you can introduce a dummy root tag.
var myxml:XML = new XML("<root>" + ldr.data + "</root>");
trace(myxml.data.toString()); //Hello
trace(myxml.value.toString()); //Hi
}
page.jsp should serialize the collection to appropriate format (xml/json/whatever) and return it.