AS2 Refresher: sendAndLoad with LoadVars
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 a response.
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 with LoadVars Snippet:
-
// variables
-
//——————————————————————————–
-
-
// set a ref to hold your URL
-
var myURL:String = "test.html";
-
-
// set a new LoadVars object
-
var result_lv:LoadVars = new LoadVars();
-
-
// function that fires when a result is received from the server
-
result_lv.onLoad = function(success:Boolean) {
-
-
if (success) {
-
-
trace ("——————————————————-")
-
trace (">>>>> SUCCESS!");
-
trace ("——————————————————-")
-
-
} else {
-
-
trace ("——————————————————-")
-
trace (">>>>> ERROR!");
-
trace ("——————————————————-")
-
}
-
};
-
-
// instantiate a new LoadVars object
-
var myLoadVars:LoadVars = new LoadVars();
-
-
// assign parameters to the object
-
myLoadVars.var1 = "Robots";
-
myLoadVars.var2 = "Aliens";
-
-
// send our variables to the value in myURL and await the response which will be caught by ‘result_lv.onLoad’
-
myLoadVars.sendAndLoad(myURL, result_lv, "POST");
-
-
// make sure your URL is configured correctly by tracing out the compiled syntax
-
trace("posting to: " + myURL + "?var1=" + myLoadVars.var1 + "&var2=" + myLoadVars.var2);


