【发布时间】:2017-10-24 12:14:43
【问题描述】:
我在登录后使用 oauth 后端生成有效令牌,但我返回 2 个单独的 json 对象,因为当我创建令牌并从我的数据库返回用户数据时,我无法将它们与 PHP 链接,所以我得到一个像这样的字符串:
{"id":"1","email":"test@test.com","username":"user","firstname":"john","lastname":"doe"} {"access_token":"d0f8...2a","expires_in":3600,"token_type":"Bearer","scope":null}
如您所见,我得到了 2 个单独的 JSON 对象,它们也都在一行上,所以我不能用新行分割,而且我觉得分割这些数据只会弄得一团糟
在 Angular 2 中,从中获取 2 个 JSON 对象的最佳方法是什么?
这是我返回数据的方式:
login() {
this.loading = true;
this.authenticationService.login(this.model.username, this.model.password).subscribe(
(data) => {
if(data) {
console.log(data);
}
/*this.authenticationService.token = (data.token)
if(data.token) {
localStorage.setItem('currentUser', data.token);
this.loading = false;
this.router.navigate(['/user']);
}else{
}*/
}, //changed
(err)=> {
console.log(err);
this.error = 'Username or password is incorrect';
this.loading = false;},
()=>console.log("Done")
);
}
【问题讨论】:
-
因为当我创建令牌并从我的数据库返回用户数据时我无法将它们与 PHP 链接,所以我得到了这样的字符串?代码是写在哪里的?