as代码参考:
package {
import flash.display.MovieClip;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.events.*;
import flash.net.*;
public class T4 extends MovieClip {
private var _at:String = "";
public function T4() {
addEventListener(Event.ADDED_TO_STAGE, addedHandler);
}
private function addedHandler(e:Event):void {
removeEventListener(Event.ADDED_TO_STAGE, addedHandler);
btnSave.addEventListener(MouseEvent.CLICK, btnSaveClickHandler);
btnLoad.addEventListener(MouseEvent.CLICK, btnLoadClickHandler);
}
private function btnSaveClickHandler(e:MouseEvent):void {
if (_at != "" ) return;
_at = "保存";
var urlLoader:URLLoader = new URLLoader();
var urlRequest:URLRequest = new URLRequest();
var vars:URLVariables = new URLVariables();
urlRequest.url = "articleSave.php";
vars.accessKey = "i am elight";
vars.uid = 1;
vars.upwd = "Elight";
vars.aid = 1;
vars.atitle = "This is a test";
vars.adata = escape(txt1.text);
urlRequest.method = URLRequestMethod.POST;
urlRequest.data = vars;
urlLoader.load(urlRequest);
urlLoader.addEventListener(Event.COMPLETE, ldCompleteHandle);
urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, ldSecurityErrorHandle);
urlLoader.addEventListener(IOErrorEvent.IO_ERROR, ldIoErrorHandle);
}
private function btnLoadClickHandler(e:MouseEvent):void {
if (_at != "" ) return;
_at = "加载";
var urlLoader:URLLoader = new URLLoader();
var urlRequest:URLRequest = new URLRequest();
var vars:URLVariables = new URLVariables();
urlRequest.url = "articleLoad.php";
vars.accessKey = "i am elight";
vars.uid = 1;
vars.upwd = "Elight";
vars.aid = 1;
urlRequest.method = URLRequestMethod.POST;
urlRequest.data = vars;
urlLoader.load(urlRequest);
urlLoader.addEventListener(Event.COMPLETE, ldCompleteHandle);
urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, ldSecurityErrorHandle);
urlLoader.addEventListener(IOErrorEvent.IO_ERROR, ldIoErrorHandle);
}
private function ldCompleteHandle(e:Event):void {
e.target.removeEventListener(Event.COMPLETE, ldCompleteHandle);
var loader:URLLoader = URLLoader(e.target);
var rel:*;
try {
rel = JSON.parse(loader.data);
} catch (error:Error) {
txtTs.text = _at + "数据处理失败:errorId=1";
_at = "";
return;
}
if (rel.result == "0") {
txtTs.text = _at + "数据成功!";
if (_at == "加载") {
txt2.text = unescape(rel.data);
}
} else {
txtTs.text = _at + "数据失败:" + rel.result;
}
_at = "";
}
private function ldSecurityErrorHandle(e:SecurityErrorEvent):void {
txtTs.text = "对不起,连接服务器失败(SecurityError)!";
}
private function ldIoErrorHandle(e:IOErrorEvent):void {
txtTs.text = "对不起,连接服务器失败(IoError)!";
}
}
}