【发布时间】:2019-08-22 06:42:24
【问题描述】:
我在角度服务请求中附加标头,但值未正确附加,我创建了一个拦截器来添加如下标头
@Injectable()
export class AddHeaderInterceptor implements HttpInterceptor {
constructor() {}
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
// Clone the request to add the new header
const clonedRequest = req.clone({ headers: req.headers.set('tenantId', 'r 123') });
// Pass the cloned request instead of the original request to the next handle
return next.handle(clonedRequest);
}
}
当我向服务器发送未正确附加的值时,如下图所示,未附加值“r 123”,我该如何解决?
【问题讨论】:
-
你能先确认你的拦截器被调用了吗?如果被调用,则尝试删除 r 和数字之间的空格 'r 123',然后尝试
-
req.clone({ headers: req.headers.set('tenantId', 'r 123') }) 也是正确的
-
请求是CORS pre-flight OPTIONS request。服务器需要使用适当的CORS 标头进行响应。
-
@georgeawg 是的,它是预检请求我该如何解决这个问题