未设置Angular 2 Cookie [英] Angular 2 cookie not set

查看:78
本文介绍了未设置Angular 2 Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这样使用http:

private headers = new Headers({ 'Content-Type': 'application/json' })
login(loginUser: LoginUser): Promise<User> {
        return this.http.post('http://localhost:9009/api/users/login', JSON.stringify(loginUser), { headers: this.headers })
            .toPromise()
            .then(res => res.json())
            .catch(this.handleError)
    }

这应将cookie自动设置为浏览器,例如:

This should set cookie to browser automatically, like this:

但是浏览器中未设置Cookie。

But there is no cookie set in browser.


  • 角度版本:2.4.10

  • 浏览器:Chrome 56.0.2924.87(64位)| FireFox 52.0.2(64位)

  • 语言:TypeScript 2.2.1

  • 节点(用于AoT问题):node --version = 3.10 .10

  • Angular version: 2.4.10
  • Browser: Chrome 56.0.2924.87 (64-bit) | FireFox 52.0.2 (64-bit)
  • Language: TypeScript 2.2.1
  • Node (for AoT issues): node --version = 3.10.10

响应标头:

推荐答案

我解决了我的问题问题涉及以下资源:

I solved my problem referring to the following resources:

  • Understanding and using CORS
  • How to fix CORS problems

首先,这是关于跨源的问题。我必须在Java服务器(在过滤器中)设置CORS标头,例如

First, this is a problem about Cross-origin.I must set CORS Headers at my java server(in the filter),like this:

    httpServletResponse.addHeader("Access-Control-Allow-Origin", "http://localhost:4444");
    httpServletResponse.addHeader("Access-Control-Allow-Credentials", "true");
    httpServletResponse.addHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, PUT");
    httpServletResponse.addHeader("Access-Control-Max-Age", "3600");
    httpServletResponse.addHeader("Access-Control-Allow-Headers", "Content-Type, Range");
    httpServletResponse.addHeader("Access-Control-Expose-Headers", "Accept-Ranges, Content-Encoding, Content-Length, Content-Range");

第二,我设置 withCredentials 归因我发出这样的请求:

Second, I set the withCredentials attribution when I make request,like this:

get(url: string, parmas: any): Observable<any> {
    return this.http.get(url, { search: parmas, headers: this.headers, withCredentials: true })
        .map((res: Response) => res.json())
        .do(data => console.log('server data:', data))  // debug
        .catch(this.handleError);
}

最后,谢谢@JJJ帮助我检测拼写错误。

Last, thanks @JJJ for helping me detecting of my spell errors.

这篇关于未设置Angular 2 Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆