角2 http withCredentials [英] angular 2 http withCredentials

查看:58
本文介绍了角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);
    });
}

推荐答案

尝试更改您的代码

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.

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

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