【问题标题】:In actionscript, is there a function like jQuery's ajax function, $.post()?在actionscript中,有没有像jQuery的ajax函数$.post()这样的函数?
【发布时间】:2011-09-08 05:09:17
【问题描述】:

我想通过从我的 Flash 应用程序中使用 post 或 get 参数访问日志脚本文件(例如 log.php)来保存用户操作日志。
Flash 是 Web 应用程序而不是桌面应用程序。

在 jQuery 中,javascript 可以通过以下代码访问网站上的其他文件:

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

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

我认为下面的动作脚本代码相当于 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");
}

【问题讨论】:

    标签: jquery ajax flash actionscript-3 post


    【解决方案1】:

    我认为还有 3 种方法可以做到这一点...

     1. httpService
     2. WebService
     3. RPC
    

    前两个是相同的,只是协议不同而已,在我看来,最后一个是最好的选择。这比其他两个快 8-10 倍。 您可以在 Adob​​e 网站上找到所有详细信息

    【讨论】:

    • 谢谢。但是这 3 种方式对我来说很难,可能是因为我不知道 mxml。
    【解决方案2】:

    我用你的方式为你做了一个。

    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
            }
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-02
      • 2012-03-03
      • 2011-12-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多