zoukankan      html  css  js  c++  java
  • Laya 使用get或post请求服务器

    export default class Request {
        private _http;
        private _server = "https://test.codeplanet.cc";//
        static _instance = null;
      //把自己设置为单例模式,不销毁则一直存在
        static getInstance(): Request {
            if (!Request._instance) {
                Request._instance = new Request;
            }
            return Request._instance;
        }
        set server(str: string) {
            this._server = str;
        }
        /**
         * 
         * @param url {string} url地址
         * @param data {object} 参数
         * 
         */
        post(params) {
            this._http = new Laya.HttpRequest();
            let requst = new Promise((resolve, reject) => {
                this._http.once(Laya.Event.COMPLETE, this, e => { resolve(this.onCompleteHanlder(e)) })
            })
            let data = this.parseParam(params.data) || {};
            let url = this.getServerFullUrl(params.url)
            this._http.send(url, data, 'post', 'text');
            return requst;
        }
        get(params) {
            this._http = new Laya.HttpRequest();
            let requst = new Promise((resolve, reject) => {
                this._http.once(Laya.Event.COMPLETE, this, e => { resolve(this.onCompleteHanlder(e)) })
            })
            let data = this.parseParam(params.data) || {};
            this._http.send(params.url, data, 'get', 'text');
            return requst;
        }
        private onCompleteHanlder(e: any) {
            return JSON.parse(e);
        }
        private parseParam(data) {
            var body = '';
            for (var i in data) {
                body += i + "=" + data[i] + "&"
            }
            return body.slice(0, -1);
        }
        private getServerRootUrl() {
            return this._server ? this._server : window.location.origin;
        }
        private getServerFullUrl(partUrl) {
            let root = this.getServerRootUrl();
            if (root) {
                console.log('getServerFullUrl ' + root + partUrl);
                return root + partUrl;
            }
            return partUrl;
        }
    }
    //调用
    import Request from "../tools/Request";//引用request文件
    private _request: Request = Request.getInstance();

    this._request.post({ "/home/index", {type:1} }).then(res => {
        
    })
     
  • 相关阅读:
    漏洞扫描
    端口探测
    IP探测
    kali linux基础命令
    python学习07
    python学习06
    openoffice+jquery.media.js实现Linux与Windows中文档在线预览
    Oracle10g安装包
    MyEclipse2014安装包附注册破解包、eclipse安装包
    外层div自适应内层div高度
  • 原文地址:https://www.cnblogs.com/cubesugarnuo/p/12362951.html
Copyright © 2011-2022 走看看