Background | data
P.s
Recently some netizens always ask flash and background data interaction problem, so I did some summary and collation, also want to facilitate everyone!
As far as I know, there are several ways for flash to interact with background data (if you feel incomplete, you can tell, I will add it):
1.LoadVars (XML)
2.Flash Remoting
3.Webservice
4.XMLSocket
I. Loadvars article
The reason I put XML here is because XML and loadvars data interact in much the same way that the data content is a little different when passed.
I now list the most commonly used "User password Authentication" instances in the development process to illustrate!
Flash code;
//=======================================================;
Define the Loadvars object;
var data_lv = new Loadvars ();
The user name variable and the parameter value submitted;
Data_lv.username = "Kinglong";
The password variables and parameter values submitted;
Data_lv.password = "King";
return results upon submission;
Data_lv.onload = function (success) {
Success, data submission is successful;
This only indicates whether the data transfer is successful, not the result of user authentication;
if (success) {
Trace ("Data submitted successfully!");
Result is also the actual results of user validation returns!
if (this.result== "true") {
Trace ("yes");
}else{
Trace ("no");
}
}else{
Trace ("Data submission failed!");
}
}
Data submission method invocation;
The first parameter is the page address of the submission;
The second parameter is to return the result object (as long as it is a Loadvars object);
The third parameter is the method of submission (this is similar to form form in HTML, which is divided into "post" and "get").
Data_lv.sendandload ("http://www.klstudio.com/save.asp", Data_lv, "post");
//=======================================================;
Background service end page processing and return content;
//=======================================================;
Receive flash submission over variables and receive a page submitted by the variable consistent;
Request ("username") is the Flash end username variable passed the value of "Kinglong";
Request ("password") is the flash end password variable passed the value of "Kinglong";
.... Database validation .....
If the user validates through
&result=true
If user authentication fails
&result=false
The entire page return content is the above line of content, &result to apply the flash end of the result variable;
If it is a number of return values, it is &result=xxx&result1=xxx this form can be;
If you are not clear, you can view the Flash Help document!
As for the way of XML, please check the Flash help document accordingly!
Advantages of the Loadvars approach:
1.flash code to achieve simple and convenient.
2. The service side receives the page and receives a form to come over the same processing, does not need the specialized technology, all service end program can realize!
Disadvantages of the Loadvars approach:
1. Not too many variables to pass.
2. The value passed by the variable cannot be too long.
3. Variable pass value can only use a data type of "string", the data type is single.
4. The data return value cannot have "&" characters, so the more complex return value needs to be processed by URL encoding.
Flash Remoting This data interface is among the four most efficient!
its advantages:
1. More data types are supported (converting from Application Server data types to ActionScript);
2. Large amount of data transfer;
3. Operational efficiency is among the highest in the number of existing;
4. Support for a variety of backstage is also better;
5. It also has the debugging module (Netconnection Debugger)
its disadvantages:
1. Flash Remoting MX components (this is provided free of charge) is required;
2. The need for the backend service to install the corresponding version of the Flash remoting module can be used, the module provided by MM is Java-EE and. NET two versions are to be charged, fortunately, there are two open source online (openamf,amfphp);
3. It seems that remoting support for the virtual host is not very good (you can go to Google search, there is no solution).
Flash End Code Description:(
I use the as1.0 version for example, the other version to the MM station to find)
Load Remoting component code, this is required;
#include "netservices.as"
Load Remoting Debug Module code, this is optional, with Netconnection debugger view debugging information;
#include "netdebug.as"
if (inited = = null) {
Inited = true;
Set the default gateway;
Netservices.setdefaultgatewayurl ("Http://localhost:8500/flashservices/gateway");
Establish a gateway connection;
Gateway_conn = Netservices.creategatewayconnection ();
access to a service;
MyService = Gateway_conn.getservice ("MyService", this);
}
Defines a function that invokes a method;
function getString (name) {
Call the remoting GetString method;
Myservice.getstring (name);
}
Defines the function that returns the result;
function Getstring_result (Result) {
Results are the result of the return;
Trace (result);
}
A function that defines the return state, which is optional;
function Getstring_status (Error) {
Trace ("Getstring_status");
Trace (Error.code);
Trace (error.description);
Trace (error.details);
}
Call function;
GetString ("Kinglong");
Service-side method definition(
I take ColdFusion component for example, other versions please refer to the appropriate information)
<!---file name is MYSERVICE.CFC--->
<cfcomponent displayname= "My Services" >
<!---defines the GetString method, you need to set access to remote, otherwise flash remoting cannot call this method--->
<cffunction name= "getString" access= "remote" returntype= "string" >
<cfargument name= "Name" type= "string" required= "true" >
<cfset myresult = arguments.name & ", welcome!" >
<cfreturn myresult>
</cffunction>
</cfcomponent>
Attach another flash Remoting online Help file (Flash Remoting livedocs), Remoting related information on the MM website