试图从Angular 2中的http响应中获取会话

时间:2016-08-18 21:12:26

标签: http session angular typescript

我有一个Angular 2应用程序,它运行在lite服务器上的端口8000上。我的快递应用程序(后端)在端口3000上运行。

当我将我的凭据发送到在端口3000上运行的后端时,我可以看到我已经成功登录了端口8000.但是我没有得到一个会话作为后面的响应 - 结束。以下是链接的截图:

enter image description here

正如您所看到的,回复显示为Session not found

当我成功登录端口3000时。然后我会从后端获得一个会话作为响应。以下是链接的截图:

enter image description here

此处还有通过Postman(Rest客户端)链接登录端口3000的屏幕截图:

enter image description here 以下是在端口3000链接上获取会话作为响应的屏幕截图:

enter image description here

所以现在我可以得出结论,后端没有问题。

我的问题是:我怎样才能将会话作为端口8000的响应?

这是我的代码:

app.component.ts:

export class AppComponent {
    //On page refresh the constructor gets called
    constructor(private _userService: UserService) {

        this._userService.getSession()
            .subscribe(
                res => {
                    console.log("app session ");
                    //log the response which shows a session
                    console.log(res); 
                },
                error => {
                    console.log("app session error");
                    console.log(error);
                }
            );
    }
}

login.component.ts:

export class LoginComponent {
    user: User = new User();
    loginRes: String;
    private toasterService: ToasterService;

    public toasterconfig: ToasterConfig = new ToasterConfig({
        showCloseButton: true,
        tapToDismiss: false,
        timeout: 0
    });

    constructor(private _userService: UserService, toasterService: ToasterService, private router: Router) {
        this.toasterService = toasterService;
    }

    data = {};
    onSubmit() {
        this._userService.login(this.user)
            .subscribe(
                //we are using an arrow function here
                res => {            
                    this.toasterService.pop('success', 'Success', 'You have logged in ' + res.username);
//                  I don't use navigate(), because I want a redirect where the page gets refreshed
//                  this.router.navigate(['/']); 
                    var host = location.host;
                    console.log("host");
                    console.log(host);
                    //redirects where the page also gets refreshed
                    window.location.href = "http://"+ host;
                },
                error => {
                     this.toasterService.pop('error', 'Failed', error._body);
                }
            );
    }
}

user.service.ts:

@Injectable()
export class UserService {
    private url: string;

    constructor(private _http: Http) {

    }

    login(user: User) {
        this.url = config.getEnvironmentVariable('endPoint')+'api/auth/login';
        let data = { "username": user.username, "password": user.password };
        let body = JSON.stringify(data);
        let headers = new Headers({ 'Content-Type': 'application/json' });
        let options = new RequestOptions({ headers: headers });

        console.log("login url " + this.url);
        return this._http.post(this.url, body, options)
            .map(this.extractData)
            .catch(this.handleError);
    }

    getSession() {
        this.url = config.getEnvironmentVariable('endPoint')+'api/auth/';
        console.log("session url " + this.url);
        let headers = new Headers({ 'Content-Type': 'application/json' });
        let options = new RequestOptions({ headers: headers });
        options.body = '';

        return this._http.get(this.url, options)
            .map(this.extractData)
            .catch(this.handleError);
    }

    private extractData(res: Response) {
        let body = res.json();
        return body || {};
    } 

    private handleError(error: any) {
        //the Observable catches and throws an error
        return Observable.throw(error.message || error);
    }
}

0 个答案:

没有答案