<i id='SgGpM'><tr id='SgGpM'><dt id='SgGpM'><q id='SgGpM'><span id='SgGpM'><b id='SgGpM'><form id='SgGpM'><ins id='SgGpM'></ins><ul id='SgGpM'></ul><sub id='SgGpM'></sub></form><legend id='SgGpM'></legend><bdo id='SgGpM'><pre id='SgGpM'><center id='SgGpM'></center></pre></bdo></b><th id='SgGpM'></th></span></q></dt></tr></i><div id='SgGpM'><tfoot id='SgGpM'></tfoot><dl id='SgGpM'><fieldset id='SgGpM'></fieldset></dl></div>
    1. <small id='SgGpM'></small><noframes id='SgGpM'>

      <tfoot id='SgGpM'></tfoot>
        <bdo id='SgGpM'></bdo><ul id='SgGpM'></ul>
    2. <legend id='SgGpM'><style id='SgGpM'><dir id='SgGpM'><q id='SgGpM'></q></dir></style></legend>

        处理 400 后的运行时错误

        时间:2023-09-08
            <tbody id='uQaOX'></tbody>
          • <bdo id='uQaOX'></bdo><ul id='uQaOX'></ul>
            <tfoot id='uQaOX'></tfoot>

            <legend id='uQaOX'><style id='uQaOX'><dir id='uQaOX'><q id='uQaOX'></q></dir></style></legend>

              <i id='uQaOX'><tr id='uQaOX'><dt id='uQaOX'><q id='uQaOX'><span id='uQaOX'><b id='uQaOX'><form id='uQaOX'><ins id='uQaOX'></ins><ul id='uQaOX'></ul><sub id='uQaOX'></sub></form><legend id='uQaOX'></legend><bdo id='uQaOX'><pre id='uQaOX'><center id='uQaOX'></center></pre></bdo></b><th id='uQaOX'></th></span></q></dt></tr></i><div id='uQaOX'><tfoot id='uQaOX'></tfoot><dl id='uQaOX'><fieldset id='uQaOX'></fieldset></dl></div>

                  <small id='uQaOX'></small><noframes id='uQaOX'>

                1. 本文介绍了处理 400 后的运行时错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  场景:

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

                  The username and password is authenticated using WebApi 2 token authentication. If the credentials are correct, then token is returned. But, if the credentials are incorrect, 400 bad request is returned. In my Ionic 2 project, if I get the response, I navigate to the next page. If I get the error, I show my custom error message.

                  问题:

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

                  The issue I'm facing is, if I enter the wrong credentials for the first time, the custom error message is shown. If I enter wrong credentials again, the custom error message is shown along with the ionic error message.

                  这是我的代码:

                  login.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");
                    }
                  

                  auth-service.ts

                  auth-service.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;
                        });
                    }
                  

                  截图:

                  错误

                  任何建议都会有所帮助.

                  Any advice would be helpful.

                  推荐答案

                  LoadingController 只能调用一次.

                  请注意,组件关闭后,将不再可用,必须创建另一个组件.

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

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

                  This is causing the exception. You should create new loading everytime in 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");
                    }
                  

                  这篇关于处理 400 后的运行时错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:与另一个离子卡重叠的虚拟滚动 ion-img 标签 下一篇:Angular 2 (Ionic 2):拦截 ajax 请求

                  相关文章

                  最新文章

                    • <bdo id='5Bebq'></bdo><ul id='5Bebq'></ul>

                      <i id='5Bebq'><tr id='5Bebq'><dt id='5Bebq'><q id='5Bebq'><span id='5Bebq'><b id='5Bebq'><form id='5Bebq'><ins id='5Bebq'></ins><ul id='5Bebq'></ul><sub id='5Bebq'></sub></form><legend id='5Bebq'></legend><bdo id='5Bebq'><pre id='5Bebq'><center id='5Bebq'></center></pre></bdo></b><th id='5Bebq'></th></span></q></dt></tr></i><div id='5Bebq'><tfoot id='5Bebq'></tfoot><dl id='5Bebq'><fieldset id='5Bebq'></fieldset></dl></div>

                    1. <legend id='5Bebq'><style id='5Bebq'><dir id='5Bebq'><q id='5Bebq'></q></dir></style></legend>
                      <tfoot id='5Bebq'></tfoot>

                      <small id='5Bebq'></small><noframes id='5Bebq'>