如何从离子2应用程序中的PHP服务器获取数据

时间:2016-10-12 10:55:05

标签: php angular ionic2

export class LoginPage {

login:{username?:string,password?:string} = {};   submitted = false;

构造函数(public navCtrl:NavController,public userData:UserData,private menu:MenuController){}

onLogin(form){     this.submitted = true;

if (form.valid) {
  this.userData.login(this.login.username);
  this.navCtrl.setRoot(Page1);
}

}

2 个答案:

答案 0 :(得分:0)

您必须通过AJAX异步调用PHP后端:

return $http.get("https://yourbackend.com/api/your_call")
    .then(function(response){
      /* Here you can process the backend response */
    });

答案 1 :(得分:0)

当我调用PHP服务器时,服务器用json数据对象响应我,如果状态为true,我需要导航到页面。

login(){

let headers = new Headers();
  headers.append('Content-Type', 'application/json');

let alert = this.alert.create({
            title: 'Warning',
            subTitle: 'Wrong Username or Password! Please Try Again !',
            buttons: ['OK']
            });
let loader = this.loading.create({
            content: "Checking ! Please wait...",
            duration: 1000
        });

let email = this.data.email;

let password = this.data.password;
let data = JSON.stringify({email, password});
let link = "http://"link"";


  this.http.post(link, data, {headers: headers})
      .subscribe(res => {
      this.navCtrl.setRoot(Page1);
      loader.present();
        console.log(res.json());
      }, (err) => {
        console.log(err);
      alert.present();
      });

} }