标签:
HTTP 请求与响应:
private createGameScene():void { //HTTP 1.0 var request = new egret.HttpRequest(); request.responseType = egret.HttpResponseType.TEXT; //请求二进制数据则用 HttpResponse.ARRAY_BUFFER //http 测试服务器 + Get 方式 request.open("http://httpbin.org/post",egret.HttpMethod.POST); // get 方法:request.open("http://httpbin.org/get",egret.HttpMethod.GET); request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); request.send(); //如果是带有参数的请求:get - url + ? + key1=value1&key2=value2; post - send(key1=value1&key2=value2); request.addEventListener(egret.Event.COMPLETE, this.onGetComplete, this); request.addEventListener(egret.IOErrorEvent.IO_ERROR, this.onGetIOError, this); request.addEventListener(egret.ProgressEvent.PROGRESS, this.onGetProgress, this); } private onGetComplete(event:egret.Event):void { var request = <egret.HttpRequest>event.currentTarget; console.log("get data : ",request.response); var responseLabel = new egret.TextField(); responseLabel.size = 18; responseLabel.text = "GET response: \n" + request.response.substring(0, 50) + "..."; this.addChild(responseLabel); responseLabel.x = 50; responseLabel.y = 70; } private onGetIOError(event:egret.IOErrorEvent):void { console.log("get error : " + event); } private onGetProgress(event:egret.ProgressEvent):void { console.log("get progress : " + Math.floor(100*event.bytesLoaded/event.bytesTotal) + "%"); }
跨域加载位图:
在本地搭建了一个 IIS 站点用来当资源站。
private createGameScene():void { var imgLoader:egret.ImageLoader = new egret.ImageLoader(); imgLoader.once(egret.Event.COMPLETE, this.imgLoadHandler, this); imgLoader.load("http://127.0.0.1/hero1.png"); } private imgLoadHandler(evt:egret.Event):void { var Loader:egret.ImageLoader = evt.currentTarget; var bmData:egret.BitmapData = Loader.data; //应该默认是以二进制数据来传输 var bMap:egret.Bitmap = new egret.Bitmap(bmData); this.addChild(bMap); }
但是在 WebGL 运行 createTexture 时报错,egret 教程上也说暂时不支持 WebGL?
跨域加载文件:
private createGameScene():void { //代码与普通请求完全相同 var url = "http://127.0.0.1/test.txt"; //IIS的MIME不支持教程的 json 格式,改成 txt 后浏览器才可访问,但js仍不可访问 var request:egret.HttpRequest = new egret.HttpRequest(); request.once(egret.Event.COMPLETE, this.resLoadHandler, this); request.once(egret.IOErrorEvent.IO_ERROR, this.resLoadHandler, this); request.once(egret.ProgressEvent.PROGRESS, this.progressLoadHandler, this); request.open(url, egret.HttpMethod.GET); request.send(); //js 的跨域问题.. } private resLoadHandler(evt:egret.Event):void{ switch(evt.type){ case egret.Event.COMPLETE: var request:egret.HttpRequest = evt.currentTarget; console.log("resLoadHandler: ", request.response); break; case egret.IOErrorEvent.IO_ERROR: console.log("resLoadHandler io error."); break; } } private progressLoadHandler( evt:egret.ProgressEvent):void{ console.log("progress:", + Math.floor(100 * evt.bytesLoaded / evt.bytesTotal) + "%"); }
这里没有指定 ResponseType,等搞定了跨域问题要回来验证一下。
标签:
原文地址:http://www.cnblogs.com/Daniel-Liang/p/5932941.html