为了方便使用 我对这个进行了简单封装 采用了Typescript 的方式
class HTTP{ private callback:any; private caller:any; private http:Laya.HttpRequest; constructor() { this.http = new Laya.HttpRequest; } public get(url:string,caller:any,callback:any):HTTP{ this.caller = caller; this.callback = callback; //this.http.once(Laya.Event.PROGRESS, this, this.onHttpRequestProgress); this.http.once(Laya.Event.COMPLETE, this, this.onHttpRequestComplete); this.http.once(Laya.Event.ERROR, this, this.onHttpRequestError); this.http.send(url, null, 'get', 'text'); return this; } public post(url:string,data:any,contentType:string,caller:any,callback:any):HTTP{ this.caller = caller; this.callback = callback; //this.http.once(Laya.Event.PROGRESS, this, this.onHttpRequestProgress); this.http.once(Laya.Event.COMPLETE, this, this.onHttpRequestComplete); this.http.once(Laya.Event.ERROR, this, this.onHttpRequestError); if(contentType==null){ this.http.send(url, data, 'post', 'text'); }else{ this.http.send(url, data, 'post', 'text',["content-type",contentType]); } return this; } private onHttpRequestError(e: any): void { this.callback.apply(this.caller,[{state:500,msg:e}]); } private onHttpRequestComplete(e: any): void { this.callback.apply(this.caller,[{state:200,data:this.http.data}]); } }
讯享网
使用方法如下 GET
讯享网this.http.get('http://www.baidu.com/',this,this.getDataOnSuccess); getDataOnSuccess(e){ if(e.state==200){ let d = e.data; console.info(d); }else{ alert(e.msg) } }
另外可以直接POST一个JSON对象 支持restful
this.http.post(Conf.HOST_NAME+"/airdefense/save",JSON.stringify({'username':'sss','pass':'abc123'}),"application/json;charset=UTF-8",this,this.onPostData);

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/63735.html