game-egret.URLRequestMethod

egret.URLRequestMethod 使用範例


class Main extends eui.UILayer {

    protected createChildren(): void {
        super.createChildren();
        egret.lifecycle.onPause = () => egret.ticker.pause();
        egret.lifecycle.onResume = () => egret.ticker.resume();
        egret.registerImplementation('eui.IAssetAdapter', new AssetAdapter());
        egret.registerImplementation('eui.IThemeAdapter', new ThemeAdapter());
        this.runGame();
    }

    private async loadResource() {
        let loadingView = this.stage.addChild(new LoadingUI()) as LoadingUI;
        await RES.loadConfig('resource/default.res.json', 'resource/');
        await new Promise(resolve => new eui.Theme('resource/default.thm.json', this.stage).once(eui.UIEvent.COMPLETE, resolve, this));
        await RES.loadGroup('preload', 0, loadingView);
        this.stage.removeChild(loadingView);
    }

    private async runGame() {
        await this.loadResource();
        // --- Edwin 給你最乾淨的程式碼, 從這行以下開始寫喔 ---

        // -- get 方法 --
        const loader = new egret.URLLoader;
        loader.dataFormat = egret.URLLoaderDataFormat.TEXT;
        loader.addEventListener(egret.Event.COMPLETE, (e: egret.Event) => {
            let data: egret.URLVariables = e.target.data;
            console.log(data);
        }, this);
        const request = new egret.URLRequest("http://httpbin.org/get?name=get"); //get 傳參
        request.method = egret.URLRequestMethod.GET;
        loader.load(request);
        
        // -- post 方法 --
        const loader2 = new egret.URLLoader;
        loader2.dataFormat = egret.URLLoaderDataFormat.TEXT;
        loader2.addEventListener(egret.Event.COMPLETE, (e: egret.Event) => {
            let data: egret.URLVariables = e.target.data;
            console.log(data);
        }, this);
        const request2 = new egret.URLRequest("http://httpbin.org/post");
        request2.method = egret.URLRequestMethod.POST;
        request2.data = new egret.URLVariables("name=post"); //post 傳參
        loader2.load(request2);

    }

}

spacer

沒有留言:

張貼留言