私は角度2で最初のプロジェクトを開始しましたが、サービスに問題があります。角度2のサービスを2回購読しました
フォームを送信するときにhttpサービスが呼び出されましたが、フォームを送信するとhttpリクエストが2回実行されます。
login.component.html
<form method="post" (ngSubmit)="login()">
<input type="email" [(ngModel)]="user.email" id="email" name="email" placeholder="Email" class="input input--block input--text-center" required >
<input type="password" [(ngModel)]="user.password" name="password" placeholder="Password" id="passord" class="input input--block input--text-center" required>
<input type="submit" value="Connect" class="btn btn--block">
</form>
login.component.ts
login() {
this.service.login(this.user.email, this.user.password)
.subscribe(res => {
if (res == null) {
alert("Fail");
} else {
res.password = null;
this.user = res;
alert("Welcome "+this.user.firstname+"!");
}
});
}
user.service.ts
login(email:string, password:string): Observable<User> {
let CryptoJS = require("cryptojs");
let sha512 = CryptoJS.algo.SHA512.create();
sha512.update(password);
password = sha512.finalize().toString();
return this.http.post(`${this.baseUrl}/user/login`,
{email: email.trim(), password: password.trim()},
{headers: this.headers })
.map(res => res.json())
.catch(this.handleError);
}
私は2回呼び出されたメソッドをチェックするためにいくつかのconsole.log("test");
を追加し、それが二回と呼ばれる無方法、私は、Webブラウザ
が要求 'OPTIONS'、他方' POST'の一つですか? – echonax
1つは、サーバー内のメソッドをチェックするオプション要求です。実際の要求が行われます。 – Gary