본문 바로가기

Actionscript3.0

Sending Varialbes and Handling a Returned Result


- sendToURL() 과 navigateToURL() 의 차이
- 데이터를 보내고 보낸 결과를 받아 온다. 

  1. package
  2. {
  3.         import flash.display.Sprite;
  4.         import flash.events.Event;
  5.         import flash.net.URLLoader;
  6.         import flash.net.URLRequest;
  7.         import flash.net.URLVariables;
  8.         import flash.text.TextField;
  9.  
  10.         public class SendAndLoad extends Sprite
  11.         {
  12.                 public function SendAndLoad()
  13.                 {
  14.                         initializeOutput();
  15.                         sendData();
  16.                 }
  17.                 private function initializeOutput():void
  18.                 {
  19.                         _output = new TextField();
  20.                         _output.width = stage.stageWidth;
  21.                         _output.height = stage.stageHeight;
  22.                        
  23.                         addChild(_output);
  24.                 }
  25.                 private function sendData():void
  26.                 {
  27.                         //Create a URLRequest to contain the data to send
  28.                         var request:URLRequest = new URLRequest("process.php");
  29.                        
  30.                         //Create name-value pairs to send to the server
  31.                         var varibles:URLVariables = new URLVariables();
  32.                         varibles.method = "getProductDetail";
  33.                         varibles.productId = 2;
  34.                         request.data = varibles;
  35.                        
  36.                         //Create a URLLoader to send the data and receive a response
  37.                         var loader:URLLoader = new URLLoader();
  38.                        
  39.                         //Expect the script to return URL-encoded variables
  40.                         loader.dataFormat = DataFormat.VARIABLES;
  41.                        
  42.                         //Listen for the complete event to red the server response
  43.                         loader.addEventListener(Event.COMPLETE, handleComplete);
  44.                        
  45.                         //Send the data in the URLRequest off to the script
  46.                         loader.load(request);
  47.                 }
  48.                 private function handlerComplte(event:Event):void
  49.                 {
  50.                        
  51.                         var loader:URLLoader = URLLoader(event.target);
  52.                        
  53.                         //Expect the script to return name and description variables
  54.                         //Display these values in a text field on the screen
  55.                         _output.text = "Name: " + loader.data.name + "\n" + "Description :"+loader.data.description;
  56.                 }
  57.         }
  58. }