Angular 2 - Get cookie from response

后端 未结 2 655
别跟我提以往
别跟我提以往 2020-12-28 17:49

I need help, I\'m trying to get the cookie from the response, and I can\'t find a way, I\'m pretty new with tsc and ng2.

This is the ng2 http post

re         


        
2条回答
  •  长发绾君心
    2020-12-28 18:37

    I am assuming you are using the angular2 http module to contact the server which will return an observable with the server response.

    You can use the response if you subscribe to it:

    //...
    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 
     });
    

    You could also transform the observable to a Promise:

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

    In certain response cases you might need to transform it using response.json() to retrieve and object.

    Hope this helps. If it isn't what you are looking for give me some more details and I'll try to help.

提交回复
热议问题