如何从 Angular 中的 404 Http 错误中获取 Set-Cookies?
大家好,我想登录网站。
我在此网址上发布带有有效电子邮件和密码的 formData https://www.imo.net/ members/imo_registration/login/ 和响应返回 404 错误,这一切“正确”(我不创建此服务器,但如果我在此服务器上有效登录。服务器返回 404 页面。更多图片)。
正如你所看到的,我有效地登录了这个页面,但我想从这个响应中获取 Set-Cookie。我该怎么做?
单击按钮上的登录事件:
async onLogin(id: number) {
await this.componentsUtils.loadingShow();
this.imoService.login().pipe(
tap((data: any) => {
// not print
console.log("data", data);
}),
finalize(() => this.componentsUtils.loadingHide()),
).subscribe(
res => {
// not print
console.log('HTTP res', res);
},
err => {
// here i catch error
console.log('HTTP Error', err)
},
);
}
ImoService:
export class ImoService {
private resourceUrl: string = "https://www.imo.net";
constructor(
private http: HttpClient,
) { }
login(email: string = "[email protected]", password: string = "somePassword"): Observable<any> {
const formData: any = new FormData();
formData.append('email', email);
formData.append('password', password);
return this.http.post<any>(this.resourceUrl + "/members/imo_registration/login/", formData);
}
}
控制台日志:在此处输入图像描述
网络:在此处输入图片描述
有效登录后的页面:在此处输入图片描述
Hello guys I want login to website.
I post formData with valid email and password on this Url https://www.imo.net/members/imo_registration/login/ and response return 404 error and this all "right" (I dont make this server but if i valid login on this server. Server return 404 with page. More in images).
As you can see i valid login this page but i want from this response get Set-Cookie. How can i do it?
Login event onClick button:
async onLogin(id: number) {
await this.componentsUtils.loadingShow();
this.imoService.login().pipe(
tap((data: any) => {
// not print
console.log("data", data);
}),
finalize(() => this.componentsUtils.loadingHide()),
).subscribe(
res => {
// not print
console.log('HTTP res', res);
},
err => {
// here i catch error
console.log('HTTP Error', err)
},
);
}
ImoService:
export class ImoService {
private resourceUrl: string = "https://www.imo.net";
constructor(
private http: HttpClient,
) { }
login(email: string = "[email protected]", password: string = "somePassword"): Observable<any> {
const formData: any = new FormData();
formData.append('email', email);
formData.append('password', password);
return this.http.post<any>(this.resourceUrl + "/members/imo_registration/login/", formData);
}
}
Console log: enter image description here
Network: enter image description here
Page after valid login: enter image description here
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您发出请求时,您必须向 httpClient 传递一个附加参数。
请注意,该方法现在返回一个 HttpResponse 对象,其中包含来自服务器的所有响应,而不仅仅是正文。
您可以像这样访问“Set-Cookie”标头
,并且可以使用以下命令访问响应的正文
You have to pass an additional argument to the httpClient when you are making the request.
Be careful that the method now returns an HttpResponse object containing all the response from the server and not just the body.
You can access the 'Set-Cookie' header like this
and you can access the response's body with