我正在现有的 Angular 11 应用程序中添加一个 http 拦截器。
我们有 2 个加载请求,它们按预期工作,但是当我添加没有逻辑的拦截器时,仅返回相同的请求,一个请求开始抛出 302 已找到状态。
所以,我不明白应该如何解决这个问题,或者是否有解决办法。
感谢您的帮助
import { Injectable } from '@angular/core'; import { HttpInterceptor, HttpEvent, HttpResponse, HttpRequest, HttpHandler } from '@angular/common/http'; import { Observable } from 'rxjs';
@Injectable() export class MyInterceptor implements HttpInterceptor { intercept(httpRequest: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(httpRequest); } }
据我所知,Angular 中没有办法拦截/控制带有 302 状态码的请求。如果你想控制请求,有两种方法:
对API进行更改,而不是返回302,只需返回数据,然后自己进行重定向,例如返回页面地址,然后在代码中设置新位置,如
window.location.href = apiReturnedURL;
使用表单发送数据和重定向:
<form ngNoForm [action]="googleSignInUrl" method="post">
<input type="hidden" name="filed1" [value]="filed1" />
<input type="hidden" name="filed2" [value]="filed2" />
<button type="submit">Sing In</button>
</form>
请注意 ngNoForm 指令
我想你错过了一些东西。 请仔细查看这篇文章,然后检查。我希望这对你有用 https://ultimatecourses.com/blog/intro-to-angular-http-interceptors