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 ERROR SyntaxError:在Response.push上的JSON.parse()的JSON输入的意外结束../ node_modules/@angular/http/fesm5/http.js.Body.json(http.js:719)在SafeSubscriber.p上的SafeSubscriber.next(myaccount.component.ts:41),在SafeSubscriber.push .. / node_modules / rxjs / esm5 / internal / Subscriber.js.SafeSubscriber . tryOrUnsub(Subscriber.js:196),位于SafeSubscriber.push ../ node_modules Subscriber.push上的/rxjs/_esm5/internal/Subscriber.js.SafeSubscriber.next(Provcriber.js:134)../ 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),位于ZoneDelegate.push ../ node_modules / zone的XMLHttpRequest.onLoad(http.js:1070) Object.onInvokeTask中的.js / dist / zone.js.ZoneDelegate.invokeTask(zone.js:421)(core.js:14051)
我该如何解决这个错误?