ionic에서 http 요청의 응답을 어떻게 읽을 수 있습니까? ionic에서 로그인 페이지를 만들고 있으며 json 객체에있는 PHP 코드에서 응답을 얻으려고합니다. 백엔드의 응답이 반환되었습니다. I could n't grab it in ionic.
.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\":\"[email protected]\"}}",
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);
string
백엔드에서 응답 본문을 받고 있습니다. 본문을 JSON으로 구문 분석 한 다음 사용해야합니다.
행을에서 this.data = data;
로 변경하십시오 this.data = JSON.parse(data)
.
또는 this.data = JSON.parse(data._data);
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다