如何使用角度2设置标头cookie

时间:2017-01-26 15:27:28

标签: angular cookies request ionic2 setcookie

晚。

当我想向服务器发送cookie时出现了这个问题。 我们的想法是检查用户是否登录。

这个代码

isLogin(): Promise<any> {
    let headers = new Headers;
    headers.append('Cookies', 'autologin=abcdef;'); //this the set cookie. but not set when i check in mozilla network tap

    return this.http.get(this.BASEURL + 'api/authentication/check', { headers: headers })
        .toPromise()
        .then(res => {
            console.log(res);
        })
        .catch(err => console.log(err));
}

这是请求标题。

Host: localhost:8080
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Access-Control-Request-Method: GET
Access-Control-Request-Headers: set-cookie
Origin: http://localhost:8100

此服务器代码

public function check()
    {
        if ($this->auth->loggedin())
        {
            $this->output
                ->set_status_header(200)
                ->set_content_type('application/json')
                ->set_output(json_encode($this->session->userdata()));
        } else {
            $this->output
                ->set_status_header(400)
                ->set_content_type('application/json')
                ->set_output(json_encode(['status' => FALSE]));
        }
    }

见?请求中没有cookie。帮我。这让我头疼...... XD

1 个答案:

答案 0 :(得分:0)

Header对象中添加RequestOptions对象。

isLogin(): Promise<any> {
    let headers = new Headers;
    headers.append('Cookies', 'autologin=abcdef;'); //this the set cookie. but not set when i check in mozilla network tap

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

    return this.http.get(this.BASEURL + 'api/authentication/check', options)
        .toPromise()
        .then(res => {
            console.log(res);
        })
        .catch(err => console.log(err));
}

这应该有用,虽然你还没有真正回复承诺,因为你已经解决了它(我认为)

导入请求选项:import { RequestOptions } from '@angular/http';