2017-01-26 1 views
0

밤.각도가 2 인 헤더 쿠키를 설정하는 방법

이 문제는 쿠키를 서버로 보내려 할 때 발생합니다. 아이디어는 사용자가 로그인했는지 여부를 확인하는 것입니다.

이 코드

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])); 
     } 
    } 

볼이 서버 코드? 요청에 쿠키가 없습니다. 도와주세요. 이 찔러 내게 두통 .. XD

답변

0

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';

+0

효과 없음. 그냥 json에서 서버의 응답. 및 쿠키.
요청 헤더가 첫 번째 쿠키와 동일하지만 여전히 포함되지 않은 쿠키 – Cecep

+0

흠 좋습니다. 예를 들어 '쿠키'의 이름을 'Test-Header'로 변경하면 다음을 볼 수 있습니까 아니면 둘 중 하나를 표시 할 수 있습니까? ? – Ivaro18

+0

나는 시도했지만 아무것도 얻지 못했습니다. fyi 내 콘솔에서이 오류가 발생했습니다. '교차 원점 요청 차단됨 : ....'이 요청 쿠키를 설정할 수없는 문제입니까? – Cecep

관련 문제