0
login(){
console.log("Test1");
this.authService.login(this.userForm.value)
.subscribe(
response => {
console.log("Test2");
console.log(response);
if(response === true){
this.router.navigate(['/admin']);
}else {
this.status = "error";
this.message = "Username or password is incorrect";
}
},
error => {
console.log(<any>error);
this.status = "error";
this.message = error ['message'];
}
);
}
Test1將顯示在控制檯中,但Test2不會。看來,訂閱功能從未執行。Angular 4/Laravel .subscribe()將不會執行
函數login()
將被我的Angular Form的提交按鈕調用。 如果我提交表單,會出現錯誤。在與.catch線下看...錯誤稱之爲 「服務器錯誤」
login(user: Object): Observable<any>{
return this.http.post('CENSORED LINK TO API', user)
.map((response: Response) => {
let token = response.json().token;
console.log("Response token: " + token);
if(token){
this.token = token;
localStorage.setItem('token', JSON.stringify(this.token));
return true;
} else {
return false;
}
})
.catch((error:any) => Observable.throw(error.json().error || { message: "Server Error"}));
}
here is a picture from the browser
如果你說你得到一個錯誤,它有助於添加關於錯誤的細節到你的問題。 – Markus
錯誤是來自.catch的「服務器錯誤」......請參閱第二個代碼的最後一行。我會編輯它;) –