【问题标题】:Angular2 http withCredentials doesn't workAngular2 http withCredentials 不起作用
【发布时间】:2018-01-12 07:08:58
【问题描述】:

我正在使用 Angular2 http 模块发送 http 请求。用户登录后,后端服务器将生成一个 cookie 会话,前端将使用此凭据发送请求。下面是角码:

get(url: string) {
    let options = new RequestOptions({headers: this.headers, withCredentials: true });
    return this.http.get(url, options)
  }

它适用于我在本地环境中启动我的后端服务器。但如果我连接到 AWS 上的远程后端服务器,它就不起作用。 在 chrome 网络面板中,当我连接本地服务器时,我可以在请求标头中看到以下数据:

Cookie:doxygen_width=256; grafana_sess=029b64eee4853; mongo-express=s%3AeTrefMEC2AOUe5evI0LWmQzTZcgsb.n2tP7es2RtWYfNN6lhg8ZXXAc%2FAdvswTnYBlrEhgTgM; session_id=f64d1718e54ad0a0c0bccdf782d91d; _ga=GA1.1.753746608.1498715254; linkedin_oauth_75nvlf2kt3yji2_crc=null

但是当我连接到远程服务器时,我在请求中看不到这个 cookie 标头。我还检查了那里有会话数据的登录响应数据。我对使用相同的 angularjs 代码感到困惑,为什么它不适用于远程服务器。

【问题讨论】:

    标签: angular cookies angular2-http


    【解决方案1】:

    试试这个代码:

    get(url: string) {
        let options = new RequestOptions({headers: this.headers, withCredentials: true });
        return this.http.get(url,{ headers: headers, search: params })
      }
    

    【讨论】:

    • 我不明白这里的options。似乎没有人使用这些选项。
    猜你喜欢
    • 1970-01-01
    • 2014-10-06
    • 2022-01-16
    • 1970-01-01
    • 2016-07-31
    • 2016-08-09
    • 1970-01-01
    • 2016-12-01
    • 1970-01-01
    相关资源
    最近更新 更多