2016-07-27 91 views
21

我正在尝试使用withCredentials向我的服务发送一个cookie,但无法找到如何实现它。 文档说:“如果服务器需要用户凭据,我们将在请求标题中启用它们”没有任何示例。 我尝试了几种不同的方法,但它仍然不会发送我的cookie。 这是我的代码到目前为止。angular 2 http withCredentials

private systemConnect(token) { 
    let headers = new Headers(); 
    headers.append('Content-Type', 'application/json'); 
    headers.append('X-CSRF-Token', token.token); 
    let options = new RequestOptions({ headers: headers }); 
    this.http.post(this.connectUrl, { withCredentials: true }, options).map(res => res.json()) 
    .subscribe(uid => { 
     console.log(uid); 
    }); 
} 

回答

39

试图改变像你这个代码

let options = new RequestOptions({ headers: headers, withCredentials: true });

this.http.post(this.connectUrl, <stringified_data> , options)...

正如你看到的,第二个PARAM应该是要发送的数据(使用JSON.stringify或只是'')以及三分之一参数中的所有选项。

+0

非常感谢!我希望他们会将此添加到文档中! :) – Lindstrom

+0

它已经在头文件和RequestOptions源文件的注释中) 直到官方API文档还没有准备好 - 我们必须在源代码中使用注释) –

+0

谢谢你将在未来中考虑到这一点:) – Lindstrom