我在多个地方发现了这个问题,但没有明确的解决方案。只有一个获取请求不起作用(401 未经授权),但是当我查看调试器时代码工作正常,所有其他请求都工作正常。 Chrome 和 Firefox 也能正常工作,没有问题。
这是我的拦截器服务:
@Injectable()
export class TokenInterceptor implements HttpInterceptor {
constructor(public auth: LoginService) {}
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
// Get the auth header from the service.
let authHeader = this.auth.getToken();
if (!authHeader) {
authHeader = '';
}
let authReq: any;
// Clone the request to add the new header.
if (this.auth.isAuthenticated()) {
authHeader = authHeader.replace(/^"(.*)"$/, '$1'); // Remove quotes from token start/end.
authReq = request.clone({headers: request.headers.set('Authorization', 'Bearer ' + authHeader)});
} else {
authReq = request.clone({headers: request.headers.set('Token', authHeader)});
}
// Pass on the cloned request instead of the original request.
return next.handle(authReq);
}
}
有人有什么建议吗?
最佳答案
你只需要添加 /
和 api 端点的结尾。例如,这将不起作用:
http://www.example.com/api/skill
但这会起作用:
http://www.example.com/api/skill/
关于javascript - 一个 GET 请求在 Safari 中不起作用(授权问题)-Angular 6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53611122/