简体   繁体   中英

Simulate a SESSION, with http request in Angular2

I have a situation in an angular app :

    this.http.get('myurl')
.map(res => res.json())
.subscribe(data =>{
  this.data = data;
  // resolve(this.data);
  console.log('token', this.data);

});

This script, gets me the token I need to login, but from the API, it use to fetch the token in a session variable, with another url, that doesn't have any parameters, eg : http://myserver/myAPI/getUserData

How can I simulate a session with angular 2 ?

You need to use localstorage to store the data returned from the service. or you can use a third party called ng2-cookies. Refer this link for more info

    this.http.get('myurl')
      .map(res => res.json())
      .subscribe(data =>{
        this.data = data;
        // resolve(this.data);
       console.log('token', this.data);

        // this is my second URL, that needs to find an active session
         this.http.get('my2ndurl')
           .map(res => res.json())
           .subscribe(data =>{
             this.data = data;
            console.log('token', this.data);

        });

   });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM