我在服务器上发了一个帖子,可以返回res {成功:true}
在我的帖子中,如果收到此回复,则表示我正在努力
this.successPost = true;
this.invitePost = false;
仅对这种确切的回应。 res {成功:正确}
我的ts代码始终在任何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条件中分配值。
Changing
if (res = {success: true} )
toif (res === {success: true} )
should do for you. But then you might face another problem because you are stringifying the response.Your final if condition should be like
if (res === '{"success": true}' )
希望能帮助到你。