如何在 post return 中使用特定的回复
How do use a specific responses in a post return
我有一个 post 到服务器,它可以 return res {success: true}
在我的 post 中,如果我收到此回复,我将尝试激活
this.successPost = 真;
this.invitePost = 假;
只有这个确切的回应。 res {成功:真}
我的 ts 代码总是 return 在任何 res 响应中都是 if 而不是 else。
ts
async sendInvite() {
let formData = {
"firstName" : this.data.firstName,
"lastName" : this.data.lastName,
"email" : this.data.email
};
this.httpClient.post<any>('https://8618' + formData, {
headers: { 'x-authorization-token': 'XXXXXXXXXX' }}
)
.pipe(
finalize(() => { })
).subscribe(
res => {
console.log ("res", res);
let response = JSON.stringify(res);
console.log ("response", response);
if (res = {success: true} ) {
this.presentToast(response);
this.successPost = true;
this.invitePost = false;
} else {
this.presentToast(response);
}
},
(err: HttpErrorResponse) => { // fire on offline
console.log("err.error", err.error);
this.presentToast(err.error);
}
);
}
那是因为你在 if 条件下赋值。
将 if (res = {success: true} )
更改为 if (res === {success: true} )
应该适合您。但是随后您可能会遇到另一个问题,因为您正在将响应字符串化。
你的最终 if 条件应该像 if (res === '{"success": true}' )
希望对您有所帮助。
我建议您不要对响应对象进行字符串化,而是按原样使用它。并使用相等运算符 (===) 而不是赋值运算符。
async sendInvite() {
let formData = {
"firstName" : this.data.firstName,
"lastName" : this.data.lastName,
"email" : this.data.email
};
this.httpClient.post<any>('https://8618' + formData, {
headers: { 'x-authorization-token': 'XXXXXXXXXX' }}
)
.pipe(
finalize(() => { })
).subscribe(
response => {
console.log ("response ", response );
if (res.success === true) {
this.presentToast(response);
this.successPost = true;
this.invitePost = false;
} else {
this.presentToast(response);
}
},
(err: HttpErrorResponse) => { // fire on offline
console.log("err.error", err.error);
this.presentToast(err.error);
}
);
}
我有一个 post 到服务器,它可以 return res {success: true}
在我的 post 中,如果我收到此回复,我将尝试激活
this.successPost = 真;
this.invitePost = 假;
只有这个确切的回应。 res {成功:真}
我的 ts 代码总是 return 在任何 res 响应中都是 if 而不是 else。
ts
async sendInvite() {
let formData = {
"firstName" : this.data.firstName,
"lastName" : this.data.lastName,
"email" : this.data.email
};
this.httpClient.post<any>('https://8618' + formData, {
headers: { 'x-authorization-token': 'XXXXXXXXXX' }}
)
.pipe(
finalize(() => { })
).subscribe(
res => {
console.log ("res", res);
let response = JSON.stringify(res);
console.log ("response", response);
if (res = {success: true} ) {
this.presentToast(response);
this.successPost = true;
this.invitePost = false;
} else {
this.presentToast(response);
}
},
(err: HttpErrorResponse) => { // fire on offline
console.log("err.error", err.error);
this.presentToast(err.error);
}
);
}
那是因为你在 if 条件下赋值。
将 if (res = {success: true} )
更改为 if (res === {success: true} )
应该适合您。但是随后您可能会遇到另一个问题,因为您正在将响应字符串化。
你的最终 if 条件应该像 if (res === '{"success": true}' )
希望对您有所帮助。
我建议您不要对响应对象进行字符串化,而是按原样使用它。并使用相等运算符 (===) 而不是赋值运算符。
async sendInvite() {
let formData = {
"firstName" : this.data.firstName,
"lastName" : this.data.lastName,
"email" : this.data.email
};
this.httpClient.post<any>('https://8618' + formData, {
headers: { 'x-authorization-token': 'XXXXXXXXXX' }}
)
.pipe(
finalize(() => { })
).subscribe(
response => {
console.log ("response ", response );
if (res.success === true) {
this.presentToast(response);
this.successPost = true;
this.invitePost = false;
} else {
this.presentToast(response);
}
},
(err: HttpErrorResponse) => { // fire on offline
console.log("err.error", err.error);
this.presentToast(err.error);
}
);
}