以下是记录的响应:
HttpResponse { headers: HttpHeaders, status: 200, statusText: "OK", ok: true, … }
其中,HttpHeaders包含以下属性: lazyInit: ƒ () lazyUpdate: null normalizedNames: Map(0) {} proto: Object ok: true status: 200 statusText: "OK" type: 4
我们也在服务器端添加了“Access-control-expose-header”,但我仍然无法获取响应。不确定是否在拦截器方法中漏掉了什么。
以下是拦截器方法:
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
req = req.clone({
setHeaders: {
'Accept': 'application/vnd.employee.tonguestun.com; version=1',
'Content-Type': 'application/json',
}
});
return next.handle(req)
.do((ev: HttpEvent<any>) => {
console.log(ev)
if (ev instanceof HttpResponse) {
console.log(ev.headers);
}
// return ev;
})
}
网络标签中的自定义标题
access-control-allow-credentials: true
access-control-allow-methods: GET, POST, OPTIONS
access-control-allow-origin: *
access-control-expose-headers: access-token,client,expiry,token-type,uid
access-token: haIXZmNWSdKJqN2by7JH1g
cache-control: max-age=0, private, must-revalidate
client: j_2dxD7NVMjGeX8BbBuELA
content-type: application/json; charset=utf-8
expiry: 1525931943
这是我的服务调用,也在调用中添加了观察“响应”。
服务调用
return this.http.post(`${environment.baseUrl}${environment.signup}`, data, {observe: "response"})
.map(response => response);
AppModule.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { AppComponent } from './app.component';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { HttpClientModule } from '@angular/common/http';
import { HTTP_INTERCEPTORS } from '@angular/common/http';
import { AppInterceptor } from './services/app-interceptor.service';
import { HomeModule } from './home/home.module';
import { SharedModule } from './shared/shared.module';
import { AppRoutingModule } from './app-routing.module';
import { AuthService } from './services/auth.service';
import { AuthGuardService } from './services/auth-guard.service';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
FormsModule,
ReactiveFormsModule,
CommonModule,
HttpClientModule,
BrowserAnimationsModule,
HomeModule,
SharedModule,
AppRoutingModule,
],
providers: [
AuthService,
AuthGuardService,
{
provide: HTTP_INTERCEPTORS,
useClass: AppInterceptor,
multi: true
},
],
bootstrap: [AppComponent]
})
export class AppModule { }
请帮我一下,谢谢!