开发者

In actionscript, is there a function like jQuery's ajax function, $.post()?

开发者 https://www.devze.com 2023-04-03 22:25 出处:网络
I want to save logs of user\'s operation by accessing log script file(for example, log.php) with post or get parameter from my flash application.

I want to save logs of user's operation by accessing log script file(for example, log.php) with post or get parameter from my flash application.

The flash is web application not desktop application.

In jQuery, javascript can access other files on the web site by using the following code:

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

$.post's document:

http://api.jquery.com/jQuery.post/

I think the following actionscript code is equivalent to the jQuery's $.post().

Does this code cause any problems which jQuery's $.post() doesn't?

Is there more simple and shorter way to do this?

var loader:URLLoader = new URLL开发者_如何学运维oader();
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");
}


I think there is 3 more way to do the same...

 1. httpService
 2. WebService
 3. RPC

First two are identical, just a protocol difference is there and according to me last one is best option.this is 8-10 time faster than other two. You can find all details On Adobe Site


I did one for ya with the way you 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)

Then you use it so:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号