AS2 → AS3: LoadVars AS3 Equivalent

ActionScript 3 has done away with the LoadVars class and when I was updating my ContactForm for AS3 I was trying to figure out how to mimic the sendAndLoad() method that LoadVars provided. I stumbled upon an article by Peter Elst which explained how to do this so I'm going to outline the differences here.

 

Let's take a look at the old AS2 code:

 

 

Actionscript:

  1. var msg: LoadVars = new LoadVars ( );
  2. var msgSent: LoadVars = new LoadVars ( );
  3.  
  4. msg. var1 = "one";
  5. msg. var2 = "two";
  6.        
  7. msgSent. onLoad = function ($success: Boolean ): Void
  8. {
  9.     if ($success )
  10.     {
  11.         trace ( "Message sent." );
  12.     }
  13.     else
  14.     {
  15.         trace ( "Message failed." );
  16.     }
  17. };
  18.  
  19. msg. sendAndLoad ( "http://www.reintroducing.com/script.php", msgSent )

 

 And here is the AS3 equivalent:

 

Actionscript:

  1. var scriptRequest:URLRequest = new URLRequest ( "http://www.reintroducing.com/script.php" );
  2. var scriptLoader:URLLoader = new URLLoader ( );
  3. var scriptVars:URLVariables = new URLVariables ( );
  4.  
  5. scriptLoader. addEventListener (Event. COMPLETE, handleLoadSuccessful );
  6. scriptLoader. addEventListener (IOErrorEvent. IO_ERROR, handleLoadError );
  7.  
  8. scriptVars. var1 = "one";
  9. scriptVars. var2 = "two";
  10.    
  11. scriptRequest. method = URLRequestMethod. POST;
  12. scriptRequest. data = scriptVars;
  13.  
  14. scriptLoader. load (scriptRequest );
  15.  
  16. function handleLoadSuccessful ($evt:Event ): void
  17. {
  18.     trace ( "Message sent." );
  19. }
  20.  
  21. function handleLoadError ($evt:IOErrorEvent ): void
  22. {
  23.     trace ( "Message failed." );
  24. }

 

As you can see, there is a new URLVariables class that stores the information you want to pass to your script. You then pass that URLVariables instance to the URLRequest's data property. Essentially, what this does, is create a query string with all the variables appended to it. The above actually looks like this when it is sent to the server:

http://www.reintroducing.com/script.php?var1=one&var2=two

That is what the PHP script receives and then handles it on the script's end to do whatever it is that you are wanting to do. I also explicitly set the URLRequest's method to be POST by setting the URLRequestMethod.POST constant (for GET, you'd just set URLRequestMethod.GET).

 

 

http://evolve.reintroducing.com/2008/01/27/as2-to-as3/as2-%E2%86%92-as3-loadvars-as3-equivalent/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值