Send and receive data in AS3

Source: Internet
Author: User
Tags arrays constructor save file

First, review the related solutions in as2.
I believe most people use the LoadVars class.

 
1 var xianshi_lv = new LoadVars ();
2 xianshi_lv.bianliang1 = "variable 1 ";
3 xianshi_lv.bianliang2 = "variable 2 ";
4 xianshi_lv.bianliang3 = "variable 3 ";
5 xianshi_lv.sendAndLoad ("http: // 10.7.87.222/show. asp? Bianliang = "+ random (9999), xianshi_lv," post ");
6 xianshi_lv.onLoad = function (chenggong: Boolean ){
7 if (chenggong ){
8 trace (xianshi_lv)
9 };
                                                                        }


Row 1st: defines the LoadVars object. In the as2 era, this guy has finished almost all the tasks of connecting external data. INVINCIBLE!
2nd Lines 3 and 4 lines: store variables in the LoadVars object and send them directly to external scripts.
Row 5th: execute the sendAndLoad method, which indicates the load callback data after sending.
Row 3: onLoad event, which determines the load callback data,
Row 7th: checks whether the load callback is successful.

In the as2 era, the LoadVars class has completed all the tasks of storing variables, connecting external data, and accepting callback data. I have also worked on as2 for a long time. I think this method is good and it is easy to learn.
However, as3 is different. In order to clarify the division of labor, the above storage variables, external data connections, and callback data are all dispersed. To some extent, I feel a little troublesome. I don't know what adobe thinks. Maybe I haven't found adobe's true intention yet ·
Now that as3 is coming, follow his rules.
What is his rule?
========================================================== ================================
Part 1: Prepare to send data

1: The external url of the connection is considered.
In as2, write the url address and variable to the LoadVars class. There are few variables. If there are more variables, it will be a little troublesome. It is even more troublesome to modify variables dynamically. As3 simply divides this part into two parts, which are unified into the URLRequest object. Simple url addresses are represented by URLRequest. url, and variables are represented by URLRequest. data. It is much easier to modify.
Note that the url address can also be written when the URLRequest constructor is used.
2: Data transmission method, post or get.
As3 is so heartless that it should be thoroughly divided. Get the transfer method to the URLRequest object. It is represented by the method attribute of the URLRequest object.
There are also two ways to change attributes,

 
Request. method = URLRequestMethod. POST; // This definition method is used in the help file.
Request. method = "post ";


The preceding two statements are equivalent. You can try trace (URLRequestMethod. POST ). The input is post.
Since the default method of as3 is get, we recommend that you define it as post.

Part 2: Send data
1: When sending and load are integrated, a load () is used ().
In as2, there are separate load () and send (), and sendAnaLoad (). I personally think that the first two functions are useless. Since they communicate with external data, every external connection needs to check the flash status and external data status to ensure normal communication between the two sides. Load () and send () will be lost, and one will be used. ADOBE unified the remaining sendAndLoad () into load (), which not only makes the logic structure more reasonable, but also simplifies the writing.
Related syntax:

 
URLLoader () constructor
Public function URLLoader (request: URLRequest = null)


Note that the brackets are URLRequest objects. You have included url addresses, variables, and methods. Currently, the URLLoader object connects data and accepts data. The other two tasks are handed over to the URLRequest object for management. Because the URLLoader object still has a lot to do, such as listening for connection data and receiving data, whether the operation has been completed, or to what extent. There are a lot of articles here. All kinds of loading require the bytesLoaded attribute and bytesTotal attribute implementation of the URLLoader object. Therefore, the division of labor in as3.
   
The following is a typical code for preparing data and connecting external data. Accepting data processing will be discussed in the next step.

 
Var request: URLRequest = new URLRequest ("http: // 10.7.87.222/test. asp ");
// Request. contentType = "text/xml"; // you can try this code by yourself. I did not study this in detail when I was a beginner. It is only useful when passing and receiving xml data. Haha
Request. data = "bianliang = 100 ";
Request. method = "POST ";
Var loader: URLLoader = new URLLoader ();
Loader. load (request );


Attachment asp code, need to test on iis5 or above server. Save file as UTF-8 code, file name is test. asp. Located under the site root directory.

 
<% @ LANGUAGE = "JAVASCRIPT" CODEPAGE = "936" %>
<%
Var bianliang = Request ("bianliang ")
Response. Write ("& biangliang =" + bianliang)
Response. Write ("& chenggong = chenggong ")
%>


Part 3: accept data
The following two tips are displayed:
1. Data can be used by flash only after being loaded.
Therefore, you must determine whether the loading is complete.
However, the statements used are completely different from those used in AS2.
As2 use onLoad event
The as3 code is as follows:

 
Loader. addEventListener (Event. COMPLETE, completeHandler );
Function completeHandler (event: Event): void {
Trace (loader. data );
   }


At present, you only need to realize that data loading must be listened to by event.
In fact, the interaction events in as3 have all used the event listening modes of the above code.
2. You need to distinguish the data attribute of the URLRequest class from the data attribute of the URLLoader class.
The former indicates a set of transmitted variables, and the latter indicates a set of accepted data.
Don't confuse it.
==========================================
If this part is to be further explored, more things will be involved.
Including xml processing and string processing.
You can familiarize yourself with the above.
Then I learned how to process the data for flash.
I will not talk about it too much, because I have already run my questions.

Almost finished?
NO, I think there is another thing that needs to be put forward separately.
Part 4: how to organize the sent variables?
Loadvars class in as2, which saves variables with n attributes
As3 uses the data attribute of the URLRequest class to save the variable.
Use an attribute to save variables?
What's more, this data attribute is an object value. Depend on!
Theoretically, we can transmit anything in as3 to external scripts.
In reality,
Define the data attribute as a string type,
We can achieve our simple needs.
For example, multiple variables are passed.
URLRequest. data = "variable 1 = 1 & Variable 2 = 2 & Variable 3 = 3 ",
As long as some string operations are performed, you can organize the data you want to send.
On the shoulders of the station giant, let's think about it again.
If there are n variables, (the value of n can make people crazy when operating n strings)
If it is a loadvars class, there is no way to do it. Let's write it one by one honestly.
In as3, how can this problem be solved? How can this problem be solved? How can this problem be solved? How can this problem be solved? How can this problem be solved? How can this problem be solved?
Since the data attribute is an object value.
You also need to save a lot of data.
So there is a word coming out.
Array!
I got stuck here,
I will try again to explain in detail the array part, or skip it.
2 minutes .................

Now that we can learn that as3 is already a veteran of as2, let alone array operations.
If you are a beginner, you should first go to the as2 forum to find Array Information.
Oh, sorry, this guy is so cool.

The last prompt is:
If you still like the writing method of AS2.
As3 can still meet your needs.
See the URLVariables class.
The code is as follows:

 
Var variables: URLVariables = new URLVariables ();
Variables. exampleSessionId = new Date (). getTime ();
Variables. exampleUserLabel = "guest ";
Request. data = variables;


The first three sentences of code seem familiar.
The same as the loadvars class of as2,
However, you still need to assign the URLVariables object to the data attribute of the URLRequest object.


The younger brother gave me a well-behaved tutorial for the first time.
In fact, I don't know if it's a tutorial. It's just a learning note of my own.
Please correct this post.


As mentioned above, URLRequest. data is an Object value. Theoretically, everything can be passed, whether it is a visible object or another invisible object (various types), but for communication with external scripts, all we need is a string in such format as name = value, and then connected with the & symbol.
Organize variables to be passed in As3. We recommend using the URLVariables class.
To put it bluntly, it is the LoadVars class that has deprived political rights, and then renamed it the URLVariables class.
For detailed methods and attributes, refer to the help file.
The following is an example code that appears in the previous article.

 
Var request: URLLoader = new URLLoader
Var variables: URLVariables = new URLVariables ();
Variables. Variable 1 = "variable 1"
Variables. Variable 1 = "variable 2 ";
Request. data = variables;
Trace (variables)
Trace (request. data)


We should know from the trace result that variables has connected several attributes and attribute values.
At the beginning, I thought I could go to URLRequest. with the further study and experiment, we found that using arrays to organize variables to be passed is not necessary and cannot be directly implemented.
Why can't it be implemented directly? Let's look at an example.
Next we will make URLRequest. data equal to an array and read the code

 
Var my_arr: Array = new Array ();
My_arr [0] = "123"
My_arr [1] = "456"
Var request: URLLoader = new URLLoader
Request. data = my_arr
Trace (my_arr)
Trace (request. data)


All Trace results are 123,456
Such a result is definitely not usable for passing variables to external scripts.
The form of variables accepted by external scripts must first be name = value pairs, and then use & to connect.
Compared with the URLVariables class that can automatically organize variables, it is a little troublesome to use arrays.

However, we still need to see the benefits of array operations, which can be traversed and operated cyclically.
Therefore, the younger brother thinks that as long as the data you need can be expressed by one general item (or multiple parameters) or has a certain law, you can describe it using mathematical relationships, and use arrays to improve your work efficiency.
However, we still need to process the array for use.
Step 1:

 
Var my_arr: Array = new Array ();
My_arr [0] = "variable 1 = 123 ";
My_arr [1] = "variable second = 456 ";


The array items are in the form of name = value,

 
Var a: Number = my_arr.length;
Var I: Number
My_arr [a] = "";
For (I = 0; I <a; I ++ ){
My_arr [a] = my_arr [a] + my_arr [I] + "&";
}
Trace (my_arr [a]);


Traverse all Array items, connect items with the & symbol, and add them to the last entry of the array.
Trace to get variable 1 = 123 & Variable 2 = 456 &, so that the required string has been obtained.
Step 3:

 
Var my_urlloader: URLLoader = new URLLoader ()
My_urlloader.data = my_arr [a]
Trace (my_urlloader.data)


Assign a value to the last entry of the preceding array, that is, variable 1 = 123 & Variable 2 = 456 &, to my_urlloader.data


It seems very troublesome.
I'm bored, so I gave the Array class prototypeNew method toloadvarsThe steps mentioned above have been integrated as follows:

 
Array. prototype. toloadvars = function (URLrequest: URLRequest ){
Var I: Number
Var a: Number = this. length
This [a] = ""
For (I = 0; I <a; I ++ ){
This [a] = this [a] + this [I] + "&"
       }
URLrequest. data = this [a]
Return URLrequest. data
};


Let's see how it works? A newbie needs to watch it again. Don't laugh at me
For example

 
Array. prototype. toloadvars = function (URLrequest: URLRequest ){
Var I: Number
Var a: Number = this. length
This [a] = ""
For (I = 0; I <a; I ++ ){
This [a] = this [a] + this [I] + "&"
       }
URLrequest. data = this [a]
Return URLrequest. data
};
Var arr: Array = new Array ();
Arr [0] = "a = 123"
Arr [1] = "B = 456"
Arr [2] = "c = 789"
Arr [3] = "d = abc"
Var my_url: URLRequest = new URLRequest ()
Arr. toloadvars (my_url );
Trace (my_url.data)


Step 1: Copy the newly defined toloadvars method to the top of the action panel,
Step 2: Defines an array to organize Variables. Note the format. Here we can see that the individual has played a role. It can greatly improve the efficiency.
Step 3: Define the URLRequest object
Step 4: Call the toloadvars method of the array. The passed parameter is the my_url object.
Then start executing the custom toloadvars method,
This method automatically connects every variable defined in the array with &, and then sends the connected result to the URLrequest. data attribute.

Finally, we recommend that you define the url and method of the URLRequest object, organize the variables using arrays, and use the toloadvars method. This idea will be a little clearer and will not be messy.

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.