想要将我唯一的令牌保存在xauth中,但是它将所有内容保存在其中?

时间:2018-11-25 12:20:27

标签: java spring angular angular6

 onlogin() {
 this.loginService.sendCredential(this.credential.username, 
 this.credential.password).subscribe(
    res => {
    localStorage.setItem("X-Auth-Token",res.json().token);
    this.loggedIn = true;
    location.reload();
    this.router.navigate(['/home']);
}, 
    error => {
        this.loggedIn = false;
        this.loginError = true;
    }
);
}

这是我在 mylogincomponent.ts

中的onlogin()函数
import { Injectable } from '@angular/core';
import {Http, Headers} from '@angular/http';
import {Observable} from 'rxjs';
import {AppConst} from '../constants/app-const';
import {Router} from '@angular/router';

@Injectable()


export class LoginService {
private serverPath:string = AppConst.serverPath;

constructor(private http:Http, private router:Router) { }

sendCredential(username: string, password: string) {
let url = this.serverPath+'/token';
let encodedCredentials = btoa(username+":"+password);
let basicHeader = "Basic "+encodedCredentials;
let headers = new Headers({
    'Content-Type' : 'application/x-www-form-urlencoded',
    'Authorization' : basicHeader
});

return this.http.get(url, {headers: headers});
}

checkSession() {
let url = this.serverPath+'/checkSession';
let headers = new Headers({
    'x-auth-token' : localStorage.getItem('xAuthToken')
});

return this.http.get(url, {headers: headers});
}

通过运行此命令,它将在控制台中显示此错误

  

core.js:12501错误SyntaxError:JSON输入意外结束       在JSON.parse()       在Response.push ../ node_modules/@angular/http/fesm5/http.js.Body.json(http.js:719)       在SafeSubscriber._next(myaccount.component.ts:41)       在SafeSubscriber.push ../ node_modules / rxjs / _esm5 / internal / Subscriber.js.SafeSubscriber .__ tryOrUnsub(Subscriber.js:196)       在SafeSubscriber.push ../ node_modules / rxjs / _esm5 / internal / Subscriber.js.SafeSubscriber.next(Subscriber.js:134)       在Subscriber.push ../ node_modules / rxjs / _esm5 / internal / Subscriber.js.Subscriber._next(Subscriber.js:77)       在Subscriber.push ../ node_modules / rxjs / _esm5 / internal / Subscriber.js.Subscriber.next(Subscriber.js:54)       在XMLHttpRequest.onLoad(http.js:1070)       在ZoneDelegate.push ../ node_modules / zone.js / dist / zone.js.ZoneDelegate.invokeTask(zone.js:421)       在Object.onInvokeTask(core.js:14051)

如何解决此错误?

0 个答案:

没有答案