AS2 Refresher: sendAndLoad XML
The following article assumes you have a solid understanding of Flash AS2 and the Flash interface and skips all basic Flash setup instructions and explanations in effort to provide you with quick, concise information. It also assumes you are dealing with a server-side script that is set up to receive parameters and send back XML.
The AS2 refresher series aims to consolidate basic, everyday snippets for the most common tasks used by Flash developers working with legacy files. Basic explanations are included in the comments, since after all, who wants to read through a bunch of crap when you are just after the code? Enjoy.
AS2 sendAndLoad XML Snippet:
-
// variables
-
//——————————————————————————–
-
-
// set a ref to hold your URL
-
var myURL:String = "test.html";
-
-
// set a new XML object
-
var myXML:XML = new XML();
-
myXML.ignoreWhite = true;
-
myXML.onLoad = onMyXMLLoad;
-
-
// functions
-
//——————————————————————————–
-
-
// runs when the xml is successfully loaded from the url
-
function onMyXMLLoad(success:Boolean) {
-
-
if (success) {
-
-
trace("XML LOADED FROM SERVER");
-
-
// xml has been loaded, call init()
-
init();
-
-
} else {
-
-
trace("ERROR: XML NOT LOADED FROM SERVER");
-
}
-
}
-
-
// function that runs after the XML has been returned and loaded from the server
-
function init():Void {
-
-
trace("init() called");
-
trace("");
-
-
trace("myXML: \n" + myXML);
-
}
-
-
// load vars
-
//——————————————————————————–
-
-
// set a new LoadVars object
-
var myLoadVars:LoadVars = new LoadVars();
-
-
// assign parameters to the object
-
myLoadVars.var1 = "Michael";
-
myLoadVars.var2 = "Becker";
-
-
// send our variables to the value in myURL and await the response which will be stored in myXML
-
myLoadVars.sendAndLoad(myURL, myXML, "POST");
-
-
// make sure your URL is configured correctly by tracing out the compiled syntax
-
trace("posting to: "+ myURL + "?var1=" + myLoadVars.var1 + "&var2=" + myLoadVars.var2);


