An example briefly describes how to send a request to ASP on the server through the GET method:
| The code is as follows: |
Copy code |
_ Root. pushAction = function (URL ){
This. loadVariables (URL, "GET ");
}
Var ask = true;
Var pushURL = "http: // localhost/test/return. asp? Ask = "+ ask;
PushAction (pushURL );
_ Root. onEnterFrame = fuction (){
Trace (_ root. output );
} |
Here, we first define a function named pushAction and use the GET parameter in the loadVariables function. Then a Boolean variable named ask is defined to pass the variable to the server. Finally, use the pushAction function to send a GET request to an ASP page named return on the server. If you need to process such a Request, in ASP, the analysis result of the URL row is stored in the Request object, for example, in the following example:
| The code is as follows: |
Copy code |
Return. asp
<% @ Language = "JScript" %>
<%
// Declare and define the variable for receiving the request
Var str_getask = Request. QueryString ("ask ");
// Declare and define returned information variables
Var str_Msg = "Sorry! Your request tread awry .";
// If the accepted variable is true, a success message is returned.
If (str_getask = "true "){
Str_Msg = "OK! Your request succeed! ";
}
Response. write ("output =" + str_Msg ); |
%> In this way, a variable named output whose value is str_Msg is sent back to Flash Player. You can reference this variable with the output name in flash. Of course, we must note that the server takes time to process this process. Therefore, the last onEnterFrame is used to test whether a variable is returned.
LoadVariables provides a simple method for loading variables from the ASP or PHP page on the server, except for the GET method, it can also use the POST method to transmit the data submitted by the client to the server. However, when processing a large amount of client-to-server interaction information, using too many loadVariables statements will complicate the maintainability and readability of the entire program. In the Action of Flash MX, a LoadVars object is added and summarized together with XML and XMLSocket objects under the Client/Server objects. The LoadVars object is an alternative to the loadVariables action. It provides a better structure for sending and returning information to the server. An example of a typical LoadVars object is as follows:
| The code is as follows: |
Copy code |
// Construct a new LoadVars object
Myload = new LoadVars ();
// Set the action of myload for Server Verification
Myload. action = "post ";
// Verify the function connected to the server
Myload. onLoad = function (success ){
// If the connection is successful with the server...
If (success ){
// If the verification action is successful...
If (this. conn = "1 "){
// Assign the output variable in the returned object to the Dynamic Text box in which var is output in the scenario.
_ Root. output = this. output;
}
} Else {
// If the connection to the server is unsuccessful, a failure message is returned. Check whether the path is correct.
_ Root. output = "Can't connect to Server ";
}
};
// The initial value of Dynamic Text is a waiting message.
_ Root. output = "Waiting for response ..."; |
// Send the variables under myload to the server and put the data returned by the server in the myload object
| The code is as follows: |
Copy code |
Myload. sendAndLoad ("http: // localhost/test/return. asp", myload, "post ");
// Myload. sendAndLoad ("http: // localhost: 8001/test/return. php", myload, "post "); |
In this scenario, a Dynamic Text box named output in var is used to receive messages from the server. Using LoadVars, we can use the sendAndLoad method to send all the variables under a LoadVars object and store the data results returned by the server to a target object, where conn, the output is sent back from the server. We also use its onLoad event function to check whether the information is sent to the server page, and then use the data (this. conn) to verify that the information is correct. The code for the server page is as follows:
ASP return. asp:
| The code is as follows: |
Copy code |
|
<% @ Language = "JScript" %>
<%
Var str_Msg;
Var action;
Var conn;
Action = Request. Form ("action ");
If (action = "post "){
Str_Msg = "OK! Return a Msg to you! ";
Conn = 1;
}
Response. write ("conn =" + conn + "& output =" + str_Msg );
%>
PHP return. php:
<Php
$ Action = $ HTTP_POST_VARS ["action"];
If ($ action = "post "){
$ Str_Msg = "OK! Return a Msg to you! ";
$ Conn = 1;
}
Echo "conn =". $ conn. "& output =". $ str_Msg;
?>;
|
If this example is successfully executed, a success screen is displayed in Flash Player, as shown in the figure. If a failure occurs, a failure message is returned (generally a path error)