Night.
当我想将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。 帮帮我。 这卡住了让我头疼。.鑫达
在RequestOptions
对象中添加Header
对象。
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';