Angular 当响应为空时,如何获取响应头?

Angular 当响应为空时,如何获取响应头?,angular,typescript,put,dotcms,Angular,Typescript,Put,Dotcms,使用Angular2和dotcms,我尝试在调用subscribe后获取响应头 const httpOptions = { headers: new HttpHeaders({ 'Content-Type': 'application/json' }) .append('Access-Control-Expose-Headers', 'X-Custom-header') }; let json = {..}; let apiURL = 'api/content/publish.1' th

使用Angular2和dotcms,我尝试在调用subscribe后获取响应头

const httpOptions = {
  headers: new HttpHeaders({ 'Content-Type': 'application/json' })
  .append('Access-Control-Expose-Headers', 'X-Custom-header')
};
let json = {..};
let apiURL = 'api/content/publish.1'
this.http.put(apiURL, JSON.stringify(json), httpOptions).subscribe((res:Response) => console.log(res.headers.keys()));
好的,响应主体是空的,但在Chrome上我可以看到所有响应标题

HTTP/1.1 200 OK
X-Powered-By: Express
access-control-allow-origin: *
server: Apache-Coyote/1.1
location: http://localhost:3000/api/content/inode/bb23a6ac-f6f0-4ed3-8971-095dbc38ef52
inode: bb23a6ac-f6f0-4ed3-8971-095dbc38ef52
identifier: c05e9b20-46a4-4efc-9ded-b5d1a2613cad
access-control-allow-methods: GET, HEAD, POST, PUT, DELETE, OPTIONS
access-control-allow-headers: Authorization, Accept, Content-Type, 

Cookies
    content-length: 0
    date: Thu, 15 Feb 2018 12:34:27 GMT 
connection: close
我得到了200,执行很好,我不能得到响应头

ERROR TypeError: Cannot read property 'headers' of null

如果需要完整响应,可以在选项对象内设置
观察
属性以及标题

this.http.put(apiURL, JSON.stringify(json), 
 {observe:'response'}).subscribe((res:Response) => 
  console.log(res));

在这种情况下,您将得到整个响应以及标题。

我仍然得到null const httpOptions={headers:new HttpHeaders({'Content Type':'application/json'}.append('Access-Control-Expose-headers','X-Custom-header'),observer:'response'};键是observe not observer,您忘记了在append()之前结束括号.append('Access-Control-Expose-headers','X-Custom-header'),注意:'response'};非常感谢您的帮助!现在它可以使用这个.http.put(apirl,json.stringify(json),{headers:new-HttpHeaders({'Content-Type','application/json')).append('Access-Control-Expose-Headers','X-Custom-header'),注意:'response'}),但是如果我将所有选项移动到一个常量,我得到的“Type'string'不能分配给Type'body'。
this.http.put(apiURL, JSON.stringify(json), 
 {observe:'response'}).subscribe((res:Response) => 
  console.log(res));