簡體   English   中英

Angular 6:httpclient不顯示標頭,而chrome顯示

[英]Angular 6: httpclient does not display headers while chrome does

如果我的問題似乎可以解決,請原諒我。 這些對我不起作用。

基本上,我是angular 6的新用戶。我使用http post驗證用戶身份。 我的代碼如下所示。

onSubmit(value){
    const httpOptions = {
        headers: new HttpHeaders({ 'Content-Type': 'application/json' }),
        observe: 'response' as 'body'
    };

    this.http.post(Globals.authEndPoint, value, {observe: 'response'})
        .subscribe(function(res){
            console.log(res);
        });
}

在服務器端:

app.use(function(req, res, next) {
        res.header("Access-Control-Allow-Origin", "*");
        res.header("Access-Control-Allow-Headers", "*");

        if (req.method === 'OPTIONS'){
            res.header('Access-Control-Allow-Methods', 'PUT, POST, PATCH, DELETE, GET' );
            return res.status(200).json({})
        }
        next();
    });

和這個:

 res.status(200).header( 'X-Auth-Token', 'token-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x').send(user);

我的角度要求是這樣完成的:

我發現了一個像我的一樣的問題 ,但是答案對我的情況不起作用。

我想我做錯了。 我如何直接在響應后獲得令牌。

有任何想法嗎 ?

在此處輸入圖片說明

在此處輸入圖片說明

更新:

我嘗試了下面列出的答案。

getConfigResponse(value): Observable<HttpResponse<Config>> {
    return this.http.post<Config>( Globals.authEndPoint, value, {observe: 'response'});
}

這次我得到一個標題。 在此處輸入圖片說明

您是否嘗試閱讀完整的回復? 另外,如果您尚未使用Angular 4.3.x及更高版本,我建議您使用HttpClient。

HttpClient閱讀完整回復

showConfigResponse() {
  this.configService.getConfigResponse()
    // resp is of type `HttpResponse<Config>`
    .subscribe(resp => {
      // display its headers
      const keys = resp.headers.keys();
      this.headers = keys.map(key =>
        `${key}: ${resp.headers.get(key)}`);

      // access the body directly, which is typed as `Config`.
      this.config = { ... resp.body };
    });
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM