首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >尽管在angular2+中捕获错误,但仍收到了400个坏请求

尽管在angular2+中捕获错误,但仍收到了400个坏请求
EN

Stack Overflow用户
提问于 2019-01-04 07:15:41
回答 2查看 3.9K关注 0票数 2

我设计了一个登录页面,当我输入正确的登录名和密码时,登录是成功的,当我输入正确的用户名或密码时,登录不会发生。但是,我得到了以下错误:

发布http://localhost:3003/login/authenticate 400 (不良请求) 错误HttpErrorResponse{headers: HttpHeaders,status: 400,statusText:“坏请求”,url:"http://localhost:3003/login/authenticate",ok: false,…}

但是,我在控制台中得到了错误。如下所示:

我希望400坏请求错误不出现在控制台中。怎么做?

login.component.ts

代码语言:javascript
复制
login(data) {

    console.log("Inside Login");

    this.authenticateObj = {
      username: data.username,
      password: data.password
    }


    this.http.post("http://localhost:3003/login/authenticate", 
      this.authenticateObj)
      .map(Response => Response)
        .catch((err) => {
          console.log("err =", err)
         alert('Login Failed. Username or Password 
       is incorrect');
          return Observable.throw(err);
      })
      .subscribe((res: Response) => {
        console.log("Inside login authenticate subscribe");

        this.info = res;
        if (this.info.message == 'Login Successful.') {


          console.log("test after login = ", this.info);


          if (localStorage.getItem('username') && 
          localStorage.getItem('token')) {
             alert('Login Successful');
            this.router.navigate(['/file-upload/wsdl']);
          } else {
            this.notification.Error('Unauthorized');
          }
        }
        if (this.info.message == 'error') {
          alert('Login Failed');
        }
        else if (this.info.status == 400) {
          alert('Login Failed');
        }
      })


  }

login.controller.js

代码语言:javascript
复制
function authenticateUser(req, res, next) {

console.log("Inside authenticateUser = ", req.body)

    LoginService.authenticate(req,req.body)
        .then(function (token) {

            if (token) {
                res.setHeader("authorization",token.token);
                res.send({
                  message: 'Login Successful.',   

                    response: token
                });
            } else if(res.message == 'Username or Password is 
            incorrect'){ 

                res.status(401).send({
                    message: 'Unauthorized. '
                });
            }
            else { 
                console.log("inside controller, else res.status-400");  
                res.status(400).send({
                    message: 'Username or password is incorrect'
                });
            }
        })
        .catch(function (err) {
            console.log("inside controller, catch res.status 400")
            // res.status(400).send(err);

            res.status(400).send({
                message: 'Username or password is incorrect'
            });

        });
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-01-04 07:53:23

为了正确处理来自服务器的错误,您必须在subcribe()从Rxjs返回的ObservableObservable方法中捕获它们:

代码语言:javascript
复制
this.http.post("http://localhost:3003/login/authenticate", this.authenticateObj)
    .subscribe(
        (res: Response) => {
            // code when no error...
        }, 
        err => {
            // error handling...
        },
        () => {
            // finally...
        }
    ); 
票数 2
EN

Stack Overflow用户

发布于 2019-01-04 07:26:13

IMO坏请求是您的服务器对不正确的用户名/密码组合的错误响应。您可以返回一个"401“或"200”本身取决于您的需求。

现在,如果您不希望这个错误出现在控制台中,那么在您的subscribe()中添加一个错误回调。

代码语言:javascript
复制
this.http.post("http://localhost:3003/login/authenticate", this.authenticateObj)
    ...
    // rest of the code
    .subscribe((res: Response) => {
       // your code
    }, (error) => {
        // handle the error here, show some alerts, warnings, etc
        console.log(error)
    })
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54034556

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档