0
尝试我的第一次角运动。第一次从http post发送未定义的值,但第二次获得正确的响应(Edge,Firefox)。谢谢!Angular http post第一次不工作,但第二次工作
login服务(电话HTTP POST方法,并返回观察到的)
login(loginRequest: LoginRequest){
console.log("LoginService.login - userName " + loginRequest.username);
let options = new RequestOptions({ headers: headers });
return this.http.post(this.http_url, loginRequest, options).map(res =>
res.json());
LoginFormComponent(调用服务类和转换JSON以打字稿对象)
onSubmit() {
this.loginSvc.login(this.loginRequest).subscribe(
data => this.loginResponseStr = data,
error => alert(error),
() => console.log('Request completed')
);
var loginResponse = new LoginResponse();
loginResponse.fillFromJSON(JSON.stringify(this.loginResponseStr));
console.log(loginResponse.status);
console.log(loginResponse.statusDesc);
if(loginResponse.status == "SUCCESS"){
this.router.navigate(['/home-page']);
}
控制台登录
LoginService.login - userName admin main.bundle.js:370:9
undefined main.bundle.js:266:9
undefined main.bundle.js:267:9
Request completed main.bundle.js:263:181
LoginService.login - userName admin main.bundle.js:370:9
SUCCESS main.bundle.js:266:9
VALID USER main.bundle.js:267:9
Request completed main.bundle.js:263:181
HTTP调用是异步的。您需要将检查响应的代码移动到onNext处理程序中。 – hdk