javascript - 使用错误的凭据登录时收到警报
问题描述
我正在使用用户名和密码以角度在登录页面上进行制作。当我输入错误的凭据时,我收到带有用户名和密码的警报消息。
下面的 HTML 代码:
<form [formGroup]="loginForm" (ngSubmit)="onSubmit()">
<div class="alert alert-danger" role="alert" *ngIf="authenticService.loginFailed">
<span style="padding-right: 5px;"><i class="fas fa-exclamation-triangle"></i></span><span>Failed to
login!</span>
</div>
<div class="form-group">
<input type="text" class="form-control" id="txt-icon" placeholder="Username" autocomplete="off"
formControlName="userName"
[ngClass]="{ 'is-invalid': f.userName.invalid && (f.userName.dirty || f.userName.touched) }">
<div *ngIf="f.userName.invalid && (f.userName.dirty || f.userName.touched)" class="invalid-feedback">
<div *ngIf="f.userName.errors.required">Username is required</div>
</div>
</div>
<div class="form-group">
<input type="password" class="form-control" id="psw-icon" placeholder="********" autocomplete="off"
formControlName="userPassword"
[ngClass]="{ 'is-invalid': f.userPassword.invalid && (f.userPassword.dirty || f.userPassword.touched) }">
<div *ngIf="f.userPassword.invalid && (f.userPassword.dirty || f.userPassword.touched)"
class="invalid-feedback">
<div *ngIf="f.userPassword.errors.required">Password is required</div>
</div>
</div>
<div class="form-group" style="margin-top: 50px;">
<button type="submit" class="btn btn-primary btn-lg btn-login">Login</button>
</div>
</form>
登录.ts
onSubmit() {
this.authenticService.login(this.loginForm.value.userName, this.loginForm.value.userPassword)
}
正宗服务
login(uname: string, pwd: string) {
try {
const userAndPasswordBase24 = btoa(uname + ':' + pwd);
localStorage.setItem('authKey', userAndPasswordBase24);
//create authorized header
const headers = new HttpHeaders({ "Authorization" : 'Basic ' + userAndPasswordBase24, "Content-Type": "application/json"});
this.showLoader = true;
this.showError = false;
this.loginFailed = false;
this.checkLoginUser(headers).subscribe(
(user) => {
if (user) {
this.isLogin = true;
this.showLoader = false;
this.showError = false;
}
},
(error) => {
this.showLoader = false;
this.showError = true;
}
);
} catch (error) {
this.showLoader = false;
this.loginFailed = true;
}
}
请帮我解决这个问题。
截图供参考。
解决方案
在你的标题中试试这个:
headers = headers.append("X-Requested-With", "XMLHttpRequest");
推荐阅读
- angular - 如何在不多次订阅的情况下从服务中获取新的更新值
- apache-spark - 如果 Spark 支持内存溢出到磁盘,Spark Out of Memory 怎么会发生?
- ios - Siri Intent 扩展在现有应用程序中不起作用
- angular - 延迟加载时找不到模块错误
- javascript - 快速调用点击事件时随机出现类型错误
- c++ - 如何使用 std::pair 作为 std::map 中的键?
- yii2 - 如何在 yii2 中为模块创建前端和后端结构?
- python - 尝试在嵌套序列化程序中创建多个对象
- python - 如何在 Jupyter Notebook 中绘制/打印出框架?
- java - 有没有办法混合spring-security java配置和@secured注解?