Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 为什么我不能在截获的请求中获取标题?_Angular - Fatal编程技术网

Angular 为什么我不能在截获的请求中获取标题?

Angular 为什么我不能在截获的请求中获取标题?,angular,Angular,在上面的代码中,我为请求设置了“刷新令牌”头。 在httpInterceptor的拦截方法中,我在修改请求头之前记录了请求头,但是我无法在那里看到头“refresh\u token” intercept(请求:HttpRequest,下一步:HttpHandler): 可观察{ 控制台日志(“截获”); 控制台日志(请求头); const authReq=req.clone({ 标题:新的HttpHeaders({ “内容类型”:“应用程序/json”, “授权”:“持票人”+“FGDFHGFG

在上面的代码中,我为请求设置了“刷新令牌”头。 在httpInterceptor的拦截方法中,我在修改请求头之前记录了请求头,但是我无法在那里看到头“refresh\u token”

intercept(请求:HttpRequest,下一步:HttpHandler):
可观察{
控制台日志(“截获”);
控制台日志(请求头);
const authReq=req.clone({
标题:新的HttpHeaders({
“内容类型”:“应用程序/json”,
“授权”:“持票人”+“FGDFHGFGHJJGJJKJGJGHJGH”,
})
});
console.log(authReq.headers)
返回next.handle(authReq)
}

我做错了什么?

尝试设置如下标题
if(token){req=req.clone({headers:req.headers.set('Authorization','Bearer'+token)});if(!req.headers.has('Content-Type')){req=req.clone({headers:req.headers.set('Content-Type','application/json'))})
我认为以下链接会有帮助,拦截请求后设置的头工作正常。我不会在拦截之前设置头。因为您正在通过在拦截请求中创建新的HttpHeaders对象来覆盖。请尝试下面的代码
const authReq=req.clone({setHeaders:{'Content Type':'application/json','Authorization':“Bearer”+“fgdfghfghjjjjjgkjghjghjghjgh”,}}});
但我在重写它之前记录它。http get函数接收2个参数,但您在refreshToken函数中发送了3个更改行
this.http.get(this.baseUrl+“/refresh_token”,{},options)
to
this.http.get(this.baseUrl+“/refresh\u token”,options)
希望能奏效
public refreshToken(){
   let options = {

     headers:new HttpHeaders({
       "refresh_token":"djfhhfahsfkhasdfhksdfhhdff",
     })
   }
   this.http.get(this.baseUrl+"/refresh_token", options)
   .subscribe((res)=>{
     this.storage.set("access_token", res.body.access_token);
     console.log("token refreshed");
   },(err)=>{
     console.log("error refreshing token");
   });
}
intercept(req: HttpRequest<any>, next: HttpHandler):
    Observable<HttpEvent<any>> {
    console.log("intercepted");
    console.log(req.headers);
    const authReq = req.clone({
      headers: new HttpHeaders({
        'Content-Type': 'application/json',
        'Authorization': "Bearer " + "fgdfhgfghjgjghjkghkjghjghjgh",
      })
    });
    console.log(authReq.headers)
    return next.handle(authReq)

  }