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



假设我想以这种格式返回响应return ['operation' => 'success', 'data' => $row];. 我该怎么做。

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

以下是我的代码:

登录.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";         
}

回声json_encode($response);

登录.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();
}
});
});
}
}

您始终可以使用本地存储或存储来保存数据。

要存储数据:

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

要检索数据,请执行以下操作:

$ localStorage.getItem('myData');

最新更新