简体   繁体   English

$ http请求。 即使状态码为401,响应也会成功执行

[英]$http request. Response comes to success function even when status code is 401

In my Angular app I have a login function. 在我的Angular应用中,我具有登录功能。 But when I send wrong credentials and response come with status 401: Bad Credentials (there's even response.status = 401) it still goes to success. 但是,当我发送错误的凭据并且响应带有状态401:错误的凭据(甚至有response.status = 401)时,它仍然可以成功。

So I'm getting $notify "Success login and then html error page in my interceptor. That's confusing. I'm not sure what I've done to create this mess. 因此,我得到了$ notify“成功登录,然后在拦截器中显示html错误页面。这很令人困惑。我不确定要如何创建此混乱状态。

this.getTokenCustom = function (user) {
    $http.post('/login',
        JSON.stringify({username: user.username, password: user.password}))
        .then(
            function success(response) {
                localStorage.setItem('token', response.data.token);
                $.notify({message: "Success login"},{type:'success'});
                $state.go('roles');
            },
            function error(data) {
                console.log(data);
                $.notify({message: data.data.message},{type:'danger'});
            }
        );
};

UPD UPD

    this.getTokenCustom = function (user) {
    $http.post('/login',
        JSON.stringify({username: user.username, password: user.password}))
        .then(
            function success(response) {
                localStorage.setItem('token', response.data.token);
                $.notify({message: response.status + " Success login"},{type:'success'});
                $state.go('roles');
            },
            function error(data) {
                console.log(data);
                $.notify({message: data.data.message},{type:'danger'});
            }
        );
};

screen

The likely cause of this is that you may have a custom interceptor that doesn't handle error conditions properly. 造成这种情况的可能原因是,您可能有一个自定义拦截器,无法正确处理错误情况。

This post can point you on the right direction: http://www.jamessturtevant.com/posts/AngularJS-HTTP-Service-Success-Handler-400-Response-Code-and-Interceptors/ 这篇文章可以为您指明正确的方向: http : //www.jamessturtevant.com/posts/AngularJS-HTTP-Service-Success-Handler-400-Response-Code-and-Interceptors/

To quote the post: 引用帖子:

When you handle the requestError or the responseError for a interceptor and you wish to pass the error on to the next handler you must use the promise api to reject the message: 当您处理拦截器的requestError或responseError并希望将错误传递给下一个处理程序时,必须使用promise api拒绝消息:

$httpProvider.interceptors.push(function($q) {
    return {
        'requestError': function(rejection) {
            // handle same as below
        },

        'responseError': function(rejection) {
            if (canRecover(rejection)) {
                // if you can recover then don't call q.reject()
                // will go to success handlers
                return responseOrNewPromise;
            }

            // !!Important Must use promise api's q.reject()
            // to properly implement this interceptor
            // or the response will go the success handler of the caller
            return $q.reject(rejection);
        }
    };
});

A similar issue to yours was reported on Angular's Github repository and the issue had to do with a custom interceptor not handling error conditions properly. 在Angular的Github 存储库中报告了与您类似的问题 ,该问题与自定义拦截器有关,无法正确处理错误情况。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 HTTP 状态代码 401,即使我在请求中发送凭据 - HTTP status code 401 even though I’m sending credentials in the request 即使我传递了 api 令牌,请求也失败,状态代码为 401 - Request failed with status code 401 even when I passed the api token 预检的响应具有无效的HTTP状态代码:401 angular - Response for preflight has invalid HTTP status code: 401 angular 飞行前的响应具有无效的HTTP状态代码401-测试 - Response for preflight has invalid HTTP status code 401 - Testing 在mootools request.JSON中响应状态代码为http 200时调用onFailure - onFailure being called when response status code is http 200 in mootools request.JSON 获取针对Web请求的HTTP状态401未经授权? - Getting http status 401 unauthorized for the web request? iisnode在处理请求时遇到错误。 HRESULT:0x6d HTTP状态:500 HTTP子状态:1013 - iisnode encountered an error when processing the request. HRESULT: 0x6d HTTP status: 500 HTTP subStatus: 1013 请求的资源上没有“Access-Control-Allow-Origin”标头+响应具有HTTP状态代码401 - No 'Access-Control-Allow-Origin' header is present on the requested resource + The response had HTTP status code 401 React,Axios问题:预检响应具有无效的HTTP状态代码401 - React, Axios issue: Response for preflight has invalid HTTP status code 401 为什么来自 AJAX 请求的响应数据出错而不成功? - Why response data from AJAX request comes in error and not success?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM