通过ionic中的PHP API在ionic中的数组中返回响应

时间:2018-07-08 12:14:12

标签: php arrays json ionic-framework ionic3

假设我要以return ['operation' => 'success', 'data' => $row];格式返回响应。我该怎么办。

为澄清我的问题,我试图通过能够做到的php API从数据库中获取用户详细信息。我想要的是将响应和数据发送回我的 login.ts 并存储这些数据,以便我可以跨页面传递它们。

以下是我的代码:

login.php

$sql = "SELECT * FROM user WHERE username = '$username' and password='$epassword'";
  $result = mysqli_query($con,$sql);

  $row = mysqli_fetch_array($result,MYSQLI_ASSOC);

  $active = $row['active'];

  $count = mysqli_num_rows($result);
  // If result matched myusername and mypassword, table row must be 1 row                    

  if($count >0) {

 //$response = "Your Login success";
$response =  return ['operation' => 'Your Login success', 'data' => $row];

}else {

$response= "Your Login Email or Password is invalid";         

  }

echo json_encode($ response);

Login.ts

signIn(){

    //// check to confirm the username and password fields are filled

    if(this.username.value=="" ){

    let alert = this.alertCtrl.create({

    title:"ATTENTION",

    subTitle:"Username field is empty",

    buttons: ['OK']

    });

    alert.present();

    } else

    if(this.password.value==""){

    let alert = this.alertCtrl.create({

    title:"ATTENTION",

    subTitle:"Password field is empty",

    buttons: ['OK']

    });

    alert.present();

    }

    else

    {

    var headers = new Headers();

    headers.append("Accept", 'application/json');

    headers.append('Content-Type', 'application/json' );

    let options = new RequestOptions({ headers: headers });

    let data = {

    username: this.username.value,

    password: this.password.value

    };

    let loader = this.loading.create({

    content: 'Processing, please wait…',

    });

    loader.present().then(() => {

    //this.http.post('http://localhost:90/totallight/api/login.php',data,options)

    this.http.post('http://edomonitor.com/school-evaluation-api/login.php',data,options)

    .map(res => res.json())

    .subscribe(res => {
      ///resolve(res.json())
    //.then(res => res.json())
    //.then(json => console.log(json)) 

    console.log(data)

    loader.dismiss()

    if(res=="Your Login success"){



      //this.authService.setToken(data.token, data.id, data.name, data.email); 

    /*let alert = this.alertCtrl.create({

    title:"CONGRATS",

    subTitle:(res),

    buttons: ['OK']

    });

    alert.present();*/   
    }else

    {

    let alert = this.alertCtrl.create({

    title:"ERROR",

    subTitle:"Invalid username/password",

    buttons: ['OK']

    });

    alert.present();

    }

    });

    });

    }

    }

1 个答案:

答案 0 :(得分:0)

您始终可以使用localStoragestorage保存数据。

要存储数据:

$ localStorage.setItem('myData', JSON.parse(response.json());

要检索您的数据:

$ localStorage.getItem('myData');