在actionscript中,是否有像jQuery的ajax函数,$ .post()这样的函数?

时间:2011-09-08 05:09:17

标签: jquery ajax flash actionscript-3 post

我想通过使用我的flash应用程序中的post或get参数访问日志脚本文件(例如,log.php)来保存用户操作的日志。
Flash是Web应用程序而非桌面应用程序
在jQuery中,javascript可以使用以下代码访问网站上的其他文件:

$.post("test.php", {a: 1, b: 2}, function(data) {
        console.log(data);
});

$。帖子的文件:
http://api.jquery.com/jQuery.post/

我认为以下actionscript代码等同于jQuery的$ .post() 这段代码是否会导致jQuery的$ .post()没有出现任何问题? 有更简单,更短的方法吗?

var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, function():void {
    trace(loader.data);
});

var variables:URLVariables = new URLVariables();
variables.a = 1;
variables.b = 2;

var request:URLRequest = new URLRequest("test.php");
request.data = variables;
request.method = URLRequestMethod.POST;
try {
    loader.load(request);
} catch (error:Error) {
    trace("failed");
}

2 个答案:

答案 0 :(得分:0)

我认为还有3种方法可以做同样的事情......

 1. httpService
 2. WebService
 3. RPC

前两个是相同的,只有一个协议差异,根据我最后一个是最佳选择。这比其他两个快8-10倍。 您可以在Adobe网站上找到所有详细信息

答案 1 :(得分:0)

我按照 DO 的方式为你做了一个。

function HTTPPost(_URL:String,_UVar:Object,_UEvent:Object){
    var _Loader:URLLoader=new URLLoader();
    _Loader.addEventListener(Event.COMPLETE,function():void{
        if(_UEvent.hasOwnProperty("_Done"))
        _UEvent._Done(_Loader.data)
    });
    _Loader.addEventListener(IOErrorEvent.IO_ERROR,function():void{
        if(_UEvent.hasOwnProperty("_Error"))
        _UEvent._Error(_Loader.data)
    });
    var _Variables:URLVariables=new URLVariables();
    for(var i in _UVar){
        _Variables[i]=_UVar[i]
    }
    var _Request:URLRequest=new URLRequest(_URL);
    _Request.data=_Variables;
    _Request.method=URLRequestMethod.POST;
    try{
        _Loader.load(_Request);
    }catch(error:Error){
        trace("Failed.");
    }
}
//HTTPPost(File URL, post data and event functions)

然后你使用它:

HTTPPost("URL",{"Variable_1":"Value"},{
        "_Done":function(Message){
            trace(Message)//print what URL file prints
        },
        "_Error":function(Message){
            trace(Message)//print an HT with error info
        }
})