2017-02-17 13 views
2

私は、ヘルプからクッキーを取得しようとしていますが、私は道を見つけることができません、私はtscとng2でかなり新しいです。角2 - 応答からのクッキーを取得

これはNG2 HTTP POSTを

return this._http 
    .post('http://demo...', body, { headers: headers }) 
    .subscribe(
     (response: Response) => { 
      this.storeToken(response); 
     }, (err) => { 
      console.log('Error: ' + err); 
     } 
    ); 

これは、サーバーの応答である:

HTTP/1.1 200 OK 
Server: Apache-Coyote/1.1 
Access-Control-Allow-Origin: http://localhost:3000 
Access-Control-Allow-Credentials: true 
Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With 
Set-Cookie: JSESSIONID=A099CC4CA7A25DFBD12701630A7DC24C; Path=/pbcp/; HttpOnly 
Content-Type: application/json;charset=UTF-8 
Transfer-Encoding: chunked 
Date: Fri, 17 Feb 2017 04:08:15 GMT 

32 
{"status":"OK","message":"User is Authenticated."} 
0 

私は、ヘッダー配列にそれを見ることができないので、私は、困惑している...

結果console.log(response)

Response from Chrome console

console.log(response.headers)

Headers array from Chrome console

の結果は...、しかし、私はクッキーのセクションでそれを見ることができます。

Cookies section in Chrome

ありがとう!

+0

このサイズを試してください。get( "set-cookie")と私に知らせてください –

+0

また、複数のクッキーにこれを試してみましょうresHeader:Headers = res.headers; resHeader.getAll( 'set-cookie'); –

+0

こんにちは@Vinay、まず、お時間をありがとう。返信にその名前のヘッダーがないため、あなたの提案は機能しませんでした。私の編集した投稿をご覧ください。 –

答えて

10

いくつかの研究の後、私は私の側から2つの問題を発見しました。

最初にに設定した場合、クッキーは正常に設定されましたが、間違った場所で探していました。今度は私のlocalhost:3000ドメインの下でそれを探していたし、クッキーは正しくhttp://demo...ドメインの下に保存されていた。これは正しい動作だ。

enter image description here


を第二:私は次のようなリモートホスト==>詳細設定を表示==>すべてのCookieとサイトデータ... ==>とフィルタリングchrome://settings/でそれを見ることができました私は、クッキーを自動的にインクルードして受け入れるために、残りのリクエストヘッダーでもwithCredentials: trueを使用することを忘れていました。

authenticate(username: string, password: string) { 
    var body = `{"username":"${username}","password":"${password}"}`; 
    var headers = new Headers(); 
    headers.append('Content-Type', 'application/json'); 
    let options = new RequestOptions({ headers: headers, withCredentials: true }); 

    return this._http 
       .post('http://demo...', body, options) 
       .subscribe(
        (response: Response) => { 
        this.doSomething(response); 
       }, (err) => { 
        console.log('Error: ' + err); 
       }); 
} 

お返事ありがとうございました!

+1

この回答で、 'Response.headers'でSet-Cookieヘッダーにアクセスできますか? – PhoneixS

+0

Set-Cookieにアクセスする必要はありません。リクエストごとに自動的に追加されるので、' withCredentials:true'を設定してください。正常に動作します –

0

私は、あなたがサーバレスポンスで観測可能なものを返すサーバに接続するために、angle2 HTTPモジュールを使用していると仮定しています。

あなたがそれを購読する場合は、応答を使用することができます。

//... 
http.post(...your content...) 
.take(1) // optional: depending on your needs you could .take(x) this to just take a certain number of responses and then terminate the subscription in order to not have a "hot Observable" lying around 
.subscribe(response =>{ 
     .console.log(response); 
    // if I am not mistaken "response.headers" should contain the "Set-Cookie" you are looking for 
}); 

あなたはまた、約束に観測可能な変換ができます

http.post(...your content...) 
    .toPromise() 
    .then(response=>{ 
    console.log(response); 
    let headers = response.headers; 
    console.log(headers); 
    }) 
    .catch(err=>console.log(err)); 

を特定の応答のケースでは、あなたが使用してそれを変換する必要があるかもしれませんresponse.json()を呼び出すと、オブジェクトを取得してオブジェクト化します。

これが役に立ちます。あなたが探しているものでない場合は、私に詳細を教えてください。私は助けようとします。

+0

こんにちは@jparg、あなたの時間のおかげで、あなたが投稿したような投稿を使用していますが、ヘッダー配列を調べるときに "set-cookie"が表示されないので、これは機能しませんでした。私の更新されたポストを参照してください。 –

+0

それは奇妙です。あなたが:headers.getAll( "set cookie")でクッキーを取得しようとすると、あなたは空の配列を返すと思いますか? – jparg

+0

いいえ、 –

関連する問題