当响应中不存在此状态时,如何使用代码中的 Postma 中的状态
How to use status like is in Postma from code , when this status is not present in response
Service.ts
从这个函数中,我得到如下响应 JSON:
public signupuser(user: Users): Observable<boolean> {
let headers = new Headers();
headers.append('Content-Type', 'application/json');
let body = user;
return this.http.post(Api.getUrl(Api.URLS.signup), body, {
})
.pipe(map((response: Response) => {
console.log('response', response)
return true;
}));
}
JSON:
response {
"email": "usertest@gmail.com",
"nome": "user",
"cognome": "test",
"abbonato": 0,
"cityid": "22",
"msg": "Registered correctly."
}
当我从 Postman 使用它时,我得到状态 200 OK 或任何状态错误
我的问题是:当 JSON 文件中不存在此状态代码时,如何从代码中使用此状态?或者有什么想法吗?
有什么想法吗?
更新:
如何像
中那样阅读 header 中的 X-JWT
查看您的代码我相信您正在使用 Angular,默认情况下 HttpClient 观察正文而不响应。您必须将选项中的 observe
属性设置为 response
然后您应该能够读取状态。
public signupuser(user: Users): Observable<boolean> {
let headers = new Headers();
headers.append('Content-Type', 'application/json');
let body = user;
return this.http.post(Api.getUrl(Api.URLS.signup), body, { observe: 'response' })
.pipe(map((response: HttpResponse) => {
console.log('response', response);
return true;
}));
}
Service.ts
从这个函数中,我得到如下响应 JSON:
public signupuser(user: Users): Observable<boolean> {
let headers = new Headers();
headers.append('Content-Type', 'application/json');
let body = user;
return this.http.post(Api.getUrl(Api.URLS.signup), body, {
})
.pipe(map((response: Response) => {
console.log('response', response)
return true;
}));
}
JSON:
response {
"email": "usertest@gmail.com",
"nome": "user",
"cognome": "test",
"abbonato": 0,
"cityid": "22",
"msg": "Registered correctly."
}
当我从 Postman 使用它时,我得到状态 200 OK 或任何状态错误
我的问题是:当 JSON 文件中不存在此状态代码时,如何从代码中使用此状态?或者有什么想法吗?
有什么想法吗?
更新:
如何像
查看您的代码我相信您正在使用 Angular,默认情况下 HttpClient 观察正文而不响应。您必须将选项中的 observe
属性设置为 response
然后您应该能够读取状态。
public signupuser(user: Users): Observable<boolean> {
let headers = new Headers();
headers.append('Content-Type', 'application/json');
let body = user;
return this.http.post(Api.getUrl(Api.URLS.signup), body, { observe: 'response' })
.pipe(map((response: HttpResponse) => {
console.log('response', response);
return true;
}));
}