As3 note (13), successor of sendandload

Source: Internet
Author: User
Tags vars

In As2, loadvars and XML both have the sendandload method. This method can use the post method to send Parameters to the specified URL, download the server response, and accept parameters. before as3.0, it was one of the most popular data communication methods. after as3.0, the XML method has been defined according to the E4X specification, and the sendandload method has long been absent, while the loadvars class has disappeared.
In fact, as early as As2, we can find that the sendandload method is not properly designed. In the two data objects, the Data Objects exist and the mechanism is the same. now, this function is well encapsulated in as3.0, which is the debut of urlloader. the urlloader class downloads data from a URL in the form of text, binary data, or URL-encoded variables. Download text files, XML files, or other dynamic data-driven applications Program It is useful. It can be seen that in addition to the original data loading methods of variables and XML, there are still more binary data methods. This is not mentioned for the time being. Let's first look at the two sendandload methods in as2. To complete these methods, we also need the help of the following classes:

URLRequest class
Urlvariables class
The URLRequest class is used to capture all the information in a single HTTP request. The urlvariables class can transmit variables between the FLASH application and the server. For more information, see help. The following is an example of the sendandload method:
Sendandloadex.
/*@
Author: Frank
Site: www.2solo.cn
Date: 2008.02.15
Info: sendandload
*/
Package {
Import flash. Events .*;
Import flash.net .*;
Import flash. display. Sprite;

Public class sendandloadex extends sprite {
Private var xmldata: XML;
Private var ureq: URLRequest;
Private var uloader: urlloader = new urlloader ();
Private var uvar: urlvariables = new urlvariables ();
Public Function sendandloadex (){
Startload ();
}
/*
Load URL
*/
Private function startload (): void {
// Define parameters
Uvar. Param = "you are now visting www.2solo.cn"; // return parameters
Uvar. vtype = "XML"; // return type
// Uvar. vtype = "Var ";

Ureq = new URLRequest ("your url ");
// Method Type
Ureq. method = urlrequestmethod. post;
Ureq. Data = uvar;

// Define the parameter format
Uloader. dataformat = urlloaderdataformat. text;
// Uloader. dataformat = urlloaderdataformat. variables;
Uloader. addeventlistener (event. Complete, completehandler );
Uloader. addeventlistener (ioerrorevent. io_error, ioerrorhandler );
Uloader. Load (ureq );
}
/*
Loaded successfully
*/
Private function completehandler (EVT: Event): void {
Trace ("successful ");
// Trace (EVT. currenttarget. Data. vars );
Xmldata = New XML (EVT. currenttarget. data );
Parsexml ();
}
/*
Loading failed
*/
Private function ioerrorhandler (EVT: ioerrorevent): void {
Trace ("failed ");
}
/*
Parse XML
*/
Private function parsexml (){
Trace (xmldata. Child ("Param") [0]);
}
}
}

The output result of the above example is: "You are now visting www.2solo.cn". I simulated the XML. sendandload method in this example. The server uses ASP for testing,CodeAs follows:
Test. asp
<%
Response. codePage = 65001
Response. charset = "UTF-8"

If request ("vtype") = "XML" then
Response. Write "<? XML version = "" 1.0 "" encoding = "" UTF-8 ""?> <DATA>"
Response. Write "<param>" & request ("Param") & "</param>"
Response. Write "</data>"
Elseif request ("vtype") = "Var" then
Response. Write "vars =" & request ("Param ")
End if
%>

In sendandloadex. as, uvar. param and uvar. the vtype is used as two parameters to post to the Server File. uvar. param will be returned as a parameter, uvar. vtype is used as the judge to return XML or vars. in the above example, to test loadvars. sendandload:
// Uvar. vtype = "Var ";
// Uloader. dataformat = urlloaderdataformat. variables;
// Trace (EVT. currenttarget. Data. vars );
The EVT. currenttarget. Data. vars attribute is directly from the output value of test. asp. This is the same as loading the returned data using the original loadvars. Urlloaderdataformat has three types: Text, variables, and binary encoding. finally, it is worth mentioning that as3 allows operations on the file header Through urlrequestheader, which is an important improvement.

The following example shows the source file:
Http://xiazai.jb51.net/flash/sendAndLoadEX_2soloDOTcn.zip

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.