angular 2 http withCredentials [英] angular 2 http withCredentials
问题描述
我正在尝试使用 withCredentials 将 cookie 发送到我的服务,但不知道如何实现它.文档说如果服务器需要用户凭据,我们将在请求标头中启用它们"没有示例.我尝试了几种不同的方法,但它仍然不会发送我的 cookie.到目前为止,这是我的代码.
I'm am trying to use withCredentials to send a cookie along to my service but can't find out how to implement it. The docs say "If the server requires user credentials, we'll enable them in the request headers" With no examples. I have tried several different ways but it still will not send my cookie. Here is my code so far.
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);
});
}
推荐答案
尝试像这样改变你的代码
Try to change your code like this
let options = new RequestOptions({ headers: headers, withCredentials: true });
和
this.http.post(this.connectUrl, <stringified_data> , options)...
如您所见,第二个参数应该是要发送的数据(使用 JSON.stringify
或仅使用 ''
)和第三个参数中的所有选项.
as you see, the second param should be data to send (using JSON.stringify
or just ''
) and all options in one third parameter.
这篇关于angular 2 http withCredentials的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!