如何在 ionic 中读取 http 请求的响应?我正在 ionic 中创建一个登录页面,并且尝试从 json 对象中的 php 代码获取响应。后端的响应是由我无法在离子中获取它返回的。
.ts 代码
this.http.post("http://localhost:83/api/v2/signin.php", JSON.stringify(this.credentials))
.subscribe(data => {
this.data = data;
console.log(this.data);
if(data.response=="success"){
let toast = this.toastCtrl.create({
message: 'Login was successfully',
duration: 3000,
position: 'top',
cssClass: 'dark-trans',
closeButtonText: 'OK',
showCloseButton: true
});
toast.present();
this.navCtrl.setRoot(HomePage);
this.storage.set("session",response);
this.global.session=response;
}else{
this.global.loginState="login";
let alert = this.alertCtrl.create({
subTitle: data.response,
buttons: ['OK']
});
alert.present();
}
登录失败时控制台显示如下
Response {_body: "{"response":"Invalid login details entered"}"
成功后我有以下内容
Object { _body: "{\"response\":\"success\",
\"data\":{\"id\":\"4\",\"name\":\"Eli James\",
\"email\":\"eli_james@gmail.com\"}}",
status: 200, ok: true, statusText: "OK",
我的php代码是这样的:
if($row['email']==$email AND
$row['status']=='1'){
$response=array(
"response"=>"success",
"data"=>array(
"id"=>$row['id'],
"name"=>$row['name'],
"email"=>$row['email']
)
);
}else{
$response=array("response"=>"Invalid login details entered");
}
echo json_encode($response);
江户川乱折腾
缥缈止盈