javascript - 处理 400 后运行时错误

标签 javascript angular typescript ionic2

场景:

用户名和密码使用 WebApi 2 token 身份验证进行身份验证。如果凭据正确,则返回 token 。但是,如果凭据不正确,则会返回 400 错误请求。在我的 Ionic 2 项目中,如果我得到响应,我会导航到下一页。如果出现错误,我会显示我的自定义错误消息。

问题:

我面临的问题是,如果我第一次输入错误的凭据,则会显示自定义错误消息。如果我再次输入错误的凭据,自定义错误消息将与 ionic 错误消息一起显示。

这是我的代码:

登录.ts

  doLogin() {
    if (this.Username !== undefined && this.Password !== undefined) {
      this.loading.present();
      this.authSrv.login(this.Username, this.Password).subscribe(data => {
        this.navCtrl.setRoot('HomePage');
        this.loading.dismiss();
      }, (err) => {
        this.errorMsg("Invalid Username/Password !" + err);
        this.loading.dismiss();
      });
    }
    else
      this.errorMsg("Please enter Username/Password");
  }

授权服务.ts

  login(username, password) {
    let headers = new Headers();
    headers.append('Content-Type', 'application/x-www-form-urlencoded');
    let urlSearchParams = new URLSearchParams();
    urlSearchParams.append('username', username);
    urlSearchParams.append('password', password);
    urlSearchParams.append('grant_type', 'password');
    let body = urlSearchParams.toString()
    return this.http.post('http://' + this._api + '/postoken', body, { headers: headers }
    )
      .map(res => res.json())
      .map((res) => {
        if (res !== null) {
          localStorage.setItem('acess_token', res["access_token"]);
          localStorage.setItem('access_type', res["access_type"]);
          localStorage.setItem('expires_in', res["expires_in"]);
          this.loggedIn = true;
        }
        return res;
      });
  }

截屏视频:

enter image description here

错误

enter image description here

任何建议都会有所帮助。

最佳答案

LoadingController只能调用一次。

Note that after the component is dismissed, it will not be usable anymore and another one must be created.

这是导致异常的原因。 您应该每次都在 doLogin 中创建新的加载。

doLogin() {
    if (this.Username !== undefined && this.Password !== undefined) {
      this.loading = this.loadingCtrl.create({ //create here
    content: 'Please wait...'
     });
      this.loading.present();
      this.authSrv.login(this.Username, this.Password).subscribe(data => {
        this.navCtrl.setRoot('HomePage');
        this.loading.dismiss();
      }, (err) => {
        this.errorMsg("Invalid Username/Password !" + err);
        this.loading.dismiss();
      });
    }
    else
      this.errorMsg("Please enter Username/Password");
  }

关于javascript - 处理 400 后运行时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43319612/

相关文章:

angular - Typescript vs Tsc vs Ntypescript

typescript - TypeScript 类型中 bivarianceHack 的目的是什么?

c# - 使用 javascript 或 C# 在 PDF 上绘图

javascript - 在 Javascript 中返回一个字符串

javascript - Angular 8 : Reactive Form Automap and PatchValue Set Member data

angular - 对于带有 ngFor 生成的项目的 mat-menu,未调用单击函数

javascript - 如何使用:nth-child() for different background-colors when adding elements with JavaScript

javascript - 需要我的 document.write 和我的点击按钮在同一行吗?

javascript - 全局功能可通过所有应用程序使用 Angular2

javascript - 使用数组和多维对象以 Angular 显示 API 结果