调用 Http.post 不会触发 angular 中的 post 6
Calling Http.post does not trigger a post in angular 6
我根据我的模型使用 this tool 生成了我的实体组件和服务。
一切正常,但我 运行 在我的 API 中尝试登录用户时遇到了问题(功能正常)。
http.post()
方法未触发。我是 angular 的新手,不知道我做错了什么。
任何帮助都会很棒!
这是我的 UserService
的登录方法(该方法被正确调用,只有 http.post()
不工作):
/**
* Log a user by its credentials.
*/
login(username : string, password : string) : Observable<NmUser> {
let body = JSON.stringify({
'username': username,
'password': password
});
console.log(body);
return this.http.post(this.userUrl + 'login', body, httpOptions)
.pipe(
map(response => new NmUser(response)),
catchError(this.handleError)
);
} // sample method from angular doc
private handleError (error: HttpErrorResponse) {
// TODO: seems we cannot use messageService from here...
let errMsg = (error.message) ? error.message : 'Server error';
console.error(errMsg);
if (error.status === 401 ) {
window.location.href = '/';
}
return Observable.throw(errMsg);
}
调用登录函数的页面如下:
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { NmUserService } from '../../app/entities/nmUser/nmUser.service';
@Component({
selector: 'page-login',
templateUrl: 'login.html',
providers: [NmUserService]
})
export class LoginPage {
constructor(private navCtrl: NavController, private userService: NmUserService) {
}
login(username: string, password: string): void {
this.userService.login(username, password);
}
}
谢谢!
除非您订阅可观察对象,否则永远不会进行 HTTP 调用,这是在组件内完成的,如下所示:
this.userService.login(username, password).subscribe((result) => {
// This code will be executed when the HTTP call returns successfully
});
我根据我的模型使用 this tool 生成了我的实体组件和服务。
一切正常,但我 运行 在我的 API 中尝试登录用户时遇到了问题(功能正常)。
http.post()
方法未触发。我是 angular 的新手,不知道我做错了什么。
任何帮助都会很棒!
这是我的 UserService
的登录方法(该方法被正确调用,只有 http.post()
不工作):
/**
* Log a user by its credentials.
*/
login(username : string, password : string) : Observable<NmUser> {
let body = JSON.stringify({
'username': username,
'password': password
});
console.log(body);
return this.http.post(this.userUrl + 'login', body, httpOptions)
.pipe(
map(response => new NmUser(response)),
catchError(this.handleError)
);
} // sample method from angular doc
private handleError (error: HttpErrorResponse) {
// TODO: seems we cannot use messageService from here...
let errMsg = (error.message) ? error.message : 'Server error';
console.error(errMsg);
if (error.status === 401 ) {
window.location.href = '/';
}
return Observable.throw(errMsg);
}
调用登录函数的页面如下:
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { NmUserService } from '../../app/entities/nmUser/nmUser.service';
@Component({
selector: 'page-login',
templateUrl: 'login.html',
providers: [NmUserService]
})
export class LoginPage {
constructor(private navCtrl: NavController, private userService: NmUserService) {
}
login(username: string, password: string): void {
this.userService.login(username, password);
}
}
谢谢!
除非您订阅可观察对象,否则永远不会进行 HTTP 调用,这是在组件内完成的,如下所示:
this.userService.login(username, password).subscribe((result) => {
// This code will be executed when the HTTP call returns successfully
});