【问题标题】:can not access data header in build version of angular 10无法在 Angular 10 的构建版本中访问数据头
【发布时间】:2021-02-17 10:15:50
【问题描述】:

这里是我在 http 请求中的代码头:

    return this.http.post<any>(this.path.userRoot + '/logIn/random', body,
        {
            withCredentials: true,
            responseType: "json",
            observe: 'response'
        }
    )
        .pipe(map(response => {
            if (response.status === 200) {
                localStorage.setItem('auth-token', response.headers.get('auth-token'));
                localStorage.setItem('user', JSON.stringify(response.body.message));
                this.userSubject.next(response.body.message);
            }
            return response;
        }));

运行后,“response.headers.get('auth-token')”的值为空。 有没有办法解决这个问题?

【问题讨论】:

  • 你用的是cors吗?
  • 嗨,是的,我用它
  • 显示一些后端代码。看起来你没有公开标题
  • 是的,添加此标题后解决,谢谢

标签: node.js angular httprequest httpresponse


【解决方案1】:

如果使用 CORS,则需要指定 Access-Control-Expose-Headers(参见 documentation)服务器端。

Access-Control-Expose-Headers: auth-token, OtherCustomHeader

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-17
    • 1970-01-01
    • 2021-01-30
    • 1970-01-01
    • 2018-11-07
    • 2019-01-21
    • 2017-12-19
    • 1970-01-01
    相关资源
    最近更新 更多