AS3 Notes (note), Sendandload successor _flash as

Source: Internet
Author: User
Tags vars
In As2, Loadvars and XML have sendandload methods that can use the POST method to send parameters to the specified URL, download the server's response, and accept the parameters. Before As3.0, it is one of the most popular methods of data communication. After AS3.0, the XML method has been defined by reference to the E4X specification, and the Sendandload method has long been absent, and the Loadvars class has disappeared without trace.
In fact, as early as in AS2 can be found, the design of Sendandload method is very unreasonable, in the two data objects exist and the mechanism is the same. Well now, AS3.0 is a good encapsulation of this function, this is the debut of Urlloader. The Urlloader class is made up of text, binary data, or URL-encoded variable to download data from the URL. It is useful when downloading text files, XML, or other information for dynamic data-driven applications. Visible, in addition to the original variables and XML data loading methods, there are now more than binary data methods, this is not mentioned, first of all, to simulate the AS2 two Sendandload method. To complete these methods, we also need the following classes of help:

URLRequest class
Urlvariables class
The URLRequest class is used to capture all the information in a single HTTP request, and the Urlvariables class can transfer variables between the Flash application and the server. Please refer to the Help for a detailed description. The following example is an example of a Sendandload method:
Sendandloadex.as
/*@
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 {
Defining parameters
Uvar.param= "Your are now visting www.2solo.cn";//returned parameters
Uvar.vtype= "XML";//return type
Uvar.vtype = "Var";

Ureq=new urlrequest ("Your url");
Method type
Ureq.method=urlrequestmethod.post;
Ureq.data=uvar;

Define parameter formats
Uloader.dataformat=urlloaderdataformat.text;
Uloader.dataformat=urlloaderdataformat.variables;
Uloader.addeventlistener (Event.complete,completehandler);
Uloader.addeventlistener (Ioerrorevent.io_error,ioerrorhandler);
Uloader.load (Ureq);
}
/*
Load succeeded
*/
Private Function Completehandler (evt:event): void {
Trace ("Success");
Trace (Evt.currentTarget.data.vars);
Xmldata=new XML (Evt.currentTarget.data);
Parsexml ();
}
/*
Load failed
*/
Private Function Ioerrorhandler (evt:ioerrorevent): void {
Trace ("Failed");
}
/*
Parsing xml
*/
Private Function Parsexml () {
Trace (Xmldata.child ("param") [0]);
}
}
}


The output of the previous example is: "are now visting www.2solo.cn". I simulate the Xml.sendandload method in this example, the server I use ASP to do the test, the code is as 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.vtype post to server-side files for two parameters. Uvar.param is returned as a parameter, and Uvar.vtype is judged as returning XML or VARs. In the example above, To test Loadvars.sendandload, you can:
Uvar.vtype = "Var";
Uloader.dataformat=urlloaderdataformat.variables;
Trace (Evt.currentTarget.data.vars);
annotation, where the Evt.currentTarget.data.vars property comes directly from the test.asp output value, which is the same way that the original loadvars loads the returned data. There are three types of urlloaderdataformat, in addition to Text,variables, and binary has a binary encoded format. Finally, it is worth mentioning that AS3 allows the file headers to be manipulated through Urlrequestheader, This is a very important promotion.

Finally, attach this example 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.