从 ionic 中的 HTTP 请求获取响应
Get response from a HTTP request in ionic
如何在 ionic 中读取 http 请求的响应?我在 ionic 中创建一个登录页面,我试图从 json 对象中的 php 代码获取响应。后台返回的响应是我无法在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\":\"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);
告诉 HttpClient 您想要使用 post() 方法的观察选项的完整响应:
this.http.post("http://localhost:83/api/v2/signin.php", JSON.stringify(this.credentials), {观察: 'response')
.subscribe(fullResponse => {
......
现在 httpClient returns 是一个 HttpResponse 类型的 Observable 而不仅仅是正文中包含的 JSON 数据。
您正在从后端获取作为 string
的响应正文。您必须将正文解析为 JSON 然后使用它。
只需将 this.data = data;
行更改为 this.data = JSON.parse(data)
。
或this.data = JSON.parse(data._data);
如何在 ionic 中读取 http 请求的响应?我在 ionic 中创建一个登录页面,我试图从 json 对象中的 php 代码获取响应。后台返回的响应是我无法在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\":\"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);
告诉 HttpClient 您想要使用 post() 方法的观察选项的完整响应:
this.http.post("http://localhost:83/api/v2/signin.php", JSON.stringify(this.credentials), {观察: 'response') .subscribe(fullResponse => { ......
现在 httpClient returns 是一个 HttpResponse 类型的 Observable 而不仅仅是正文中包含的 JSON 数据。
您正在从后端获取作为 string
的响应正文。您必须将正文解析为 JSON 然后使用它。
只需将 this.data = data;
行更改为 this.data = JSON.parse(data)
。
或this.data = JSON.parse(data._data);