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返回

 詳細請看官方:http://layaair.ldc.layabox.com/demo/?Network_POST


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM