简体   繁体   English

使用Angular2中的http请求模拟会话

[英]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 这个脚本为我提供了我需要登录的令牌,但是从API上,它用于在会话变量中获取令牌,该变量带有另一个没有任何参数的URL,例如: http:// myserver / myAPI / getUserData

How can I simulate a session with angular 2 ? 如何模拟angular 2的会话?

You need to use localstorage to store the data returned from the service. 您需要使用localstorage来存储从服务返回的数据。 or you can use a third party called ng2-cookies. 或者您可以使用称为ng2-cookie的第三方。 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);

        });

   });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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