Laya怎么post和get数据给后台php服务器

如题所述

//测试数据传输
public data():void{
this.http = new Laya.HttpRequest(); //new一个HttpRequest类
this.http.once(Laya.Event.PROGRESS,this,this.onProgress); //数据传输中
this.http.once(Laya.Event.COMPLETE,this,this.onComplete); //数据传输完成后,会返回一个data
this.http.once(Laya.Event.ERROR,this,this.onError); //数据传输失败后返回
//post数据的写法
this.http.send("http://localhost/post.php",'name=guifa&pwd=123456', 'post', 'text');
//get数据的写法
this.http.send("http://localhost/post.php?name=guifa&pwd=12345678",null,'get', 'text');
}

//数据数据传输中触发的方法
public onProgress(e:any):void{
console.log(e);
}
//数据传输完成后,会返回一个data
public onComplete(e:any):void{
var textArea:Laya.Text = new Laya.Text(); //创建一个文本
//this.http.data就是php后台服务器返回的data值
laya.net.LocalStorage.setItem("name",this.http.data); //存储用户信息到本地上,相当于cookie
laya.net.LocalStorage.setItem("name","guifa2014"); //修改本地用户信息
var name =laya.net.LocalStorage.getItem("name"); //获取本地用户信息
var url = this.GetQueryString("url"); //获取url参数的方法
textArea.text = "cookie:"+name+"url参数:"+url;
textArea.x = 80;
textArea.y = 80;
Laya.stage.addChild(textArea); //添加文本到舞台中
}
//数据传输失败后返回
public onError(e:any):void{
console.log(e);
}

//获取url里面的参数
public GetQueryString(name):any
{
var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
if(r!=null)
return r[2]; //注意这里不能用js里面的unescape方法
return null;
}
上面是ts的代码部分
下面是服务器端测试的部分
var name = $_POST['name'];
if(!name){
echo 201;exit;
}
echo 200;exit; //php用echo返回
温馨提示:答案为网友推荐,仅供参考
相似回答