2017-01-27 2 views
1

나는 헤더 응답을 PHP에서 Token: abc으로 설정했습니다. 응답 헤더에서 값을 얻는 방법?
이 내 코드각도 2로 응답 헤더 받기

isLogin(): Promise<any> { 
     let headers = new Headers; 
     headers.set('Token', 'absajsj'); //I can set the header 
     headers.get('Token'); // But this return NULL 

     //console.log(this.cookie.get('autologin')); 

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

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

이 서버 코드. 나는 codeigniter를 사용한다.

if ($this->auth->loggedin()) 
     { 
      $this->output 
       ->set_status_header(200) 
       ->set_content_type('application/json') 
       ->set_output(json_encode(['status' => $_COOKIE['autologin']])); 
     } else { 
      $this->output 
       ->set_status_header(200) 
       ->set_header('Token: abcks') 
       ->set_content_type('application/json') 
       ->set_output(json_encode(['status' => $_SERVER['HTTP_TOKEN']])); 
     } 

블록 else에주의하십시오.

이 응답 헤더입니다. firefox dev 도구를 사용합니다.
enter image description here

답변

1

을 2 일 동안 인터넷 검색 후. 나는이 문제를 발견했다. 그리고이를 해결하기 위해 .htaccess에 Allow-Origin-Header-Expose "Token"을 추가하십시오.

저에게 감사드립니다. 이 커뮤니티는 굉장합니다. 나는 이것을 답으로 표시 할 것이다. 환호 :)

+0

"Access-Control-Expose-Headers"를 추가해야하는 약간 다른 문제가 있었지만 헤더를 언급하는 것이 도움이되었습니다. – Robert

0

대신에 추가 방법을 사용하려고 했습니까?

headers.append('Token', 'absajsj'); 
+0

예, 효과가 없습니다. : D 서버의 응답은 JSON – Cecep

+0

코드를 테스트하고 get/set이 작동합니다. '@ angular/http'에서 {Headers}를 가져 왔습니까? –

+0

죄송합니다. 이 스레드를 편집했습니다. 나는 응답 헤더에서 토큰을 얻을 수 없다는 뜻입니다. 콘솔에 null이 인쇄되었습니다. – Cecep

-1

시도하십시오 :

a.headers('Token') 
+0

주어진 오류 : 전화 유형이없는 표현식을 호출 할 수 없습니다. – Cecep

+0

약속의 res.headers 객체를 인쇄하고 게시 할 수 있습니까? –

+0

스크립트를 편집했습니다. 응답은 그림과 같습니다. 헤더에 토큰이 없습니다. – Cecep