AS / TS of the send () asynchronous request native code

Directly on the code ~~~

//服务器路径
private var _requestUrl:String = "https://127.0.0.1/service/";
private function requestLogin():void{
        var data:Object = new Object;
        data["grant_type"] = "携带的数据";
        //接口
        var interfaceStr:string="getUserInfo";
        var httpRequest:HttpRequest = new HttpRequest;
       //绑定请求完成的回调函数onMsgReturn
        httpRequest.on(Event.COMPLETE,this,onMsgReturn,[httpRequest]);
       //发送请求
        httpRequest.send(_requestUrl +interfaceStr,data,"post");
}

private function onMsgReturn(httpRequest:HttpRequest,data:Object):void{
       //解绑对应事件
        httpRequest.offAll();
        //后台返回的数据,且装换为JSON对象
        data = JSON.parse(data);
        if(data.code == 1){
              //成功怎么样......
        }else{
             //失败怎么样......
        }
}

Note: For subsequent events distribution and monitoring, updating front-end data reference
https://www.jianshu.com/p/d609448adacf


API Description:

/**
* 使用 EventDispatcher 对象注册指定类型的事件侦听器对象,以使侦听器能够接收事件通知。
* @param type       事件的类型。
* @param caller 事件侦听函数的执行域。
* @param listener   事件侦听函数。
* @param args       (可选)事件侦听函数的回调参数。
* @return 此 EventDispatcher 对象。
 */
public function on(type:String, caller:*, listener:Function, args:Array = null):EventDispatcher {
    return _createListener(type, caller, listener, args, false);
}

Guess you like

Origin blog.csdn.net/weixin_33810006/article/details/90964635