首页 文章

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

提问于
浏览
-4
export class LoginPage {

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

constructor(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

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

    return $http.get("https://yourbackend.com/api/your_call")
        .then(function(response){
          /* Here you can process the backend response */
        });
    
  • 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();
          });
    

    } }

相关问题