【问题标题】:Angular not receiving 401 and 400 http response from django rest frameworkAngular 未收到来自 django rest 框架的 401 和 400 http 响应
【发布时间】:2016-02-14 13:06:04
【问题描述】:

当我发送正确的凭据时,我会收到 HTTP_200_OK 响应,并且一切正常。但是,当我发送不正确或不完整的登录凭据时,我没有收到任何回复。这是我的代码:

Angular(authService.js 登录功能):

$http({
    method : 'POST',
    url : API_LOCATION + 'login',
    data : $.param(credentials),
    headers : {'Content-Type': 'application/x-www-form-urlencoded'}
}).then(function(response){
    console.log(response);
    if(response.status == 200){
        authService.currentUser = response.data;
        $location.path('/')
    }
    else {
        $rootScope.alert = response.data.message;
    }
});

Django rest 框架 - 登录视图:

class LoginView(views.APIView):

    serializer_class = UsersSerializer
    permission_classes = (AllowAny, )
    allowed_methods = ('GET', 'POST', 'OPTIONS', 'HEAD')

    def get(self, *args, **kwargs):
        serializer = self.serializer_class(self.request.user, context={'request' : self.request})
        return Response(serializer.data)

    def post(self, *args, **kwargs):

        username = self.request.data.get('username', None)
        password = self.request.data.get('password', None)

        if username and password:
            user = authenticate(username=username, password=password)

        else:
            print '400'
            return Response({"message": "Both fields are required"}, status=views.status.HTTP_400_BAD_REQUEST)

        if not user:
            print '401'
            return Response({"message": "Incorrect login"}, status=views.status.HTTP_401_UNAUTHORIZED)

        login(self.request, user)

        serializer = self.serializer_class(user, context={'request' : self.request})

        return Response(serializer.data, status = views.status.HTTP_200_OK)

'400' 和 '401' 分别打印在控制台中,但我没有得到任何角度响应。帮忙?

编辑

感谢 pgreen2,知道了。在错误回调函数中收到错误响应:

$http({
    method : 'POST',
    url : API_LOCATION + 'login',
    data : $.param(credentials),
    headers : {'Content-Type': 'application/x-www-form-urlencoded'}
}).then(function successCallback(response) {
    if(response.status == 200){
        authService.currentUser = response.data;
        $location.path('/')
    }
}, function errorCallback(response) {
    console.log(response)
});

【问题讨论】:

    标签: angularjs django django-rest-framework


    【解决方案1】:

    401 和 400 是错误代码,因此您需要将 .catch() 添加到您的 Angular 承诺中。错误正在发生,但您没有处理它的代码。

    更新:

    $http({
        method : 'POST',
        url : API_LOCATION + 'login',
        data : $.param(credentials),
        headers : {'Content-Type': 'application/x-www-form-urlencoded'}
    }).then(function(response){
        console.log(response);
        authService.currentUser = response.data;
        $location.path('/')
    }).catch(function(response){
        console.log(response);
        $rootScope.alert = response.data.message;
    });
    

    【讨论】:

    • 你能举个例子吗?
    • 我添加了一个快速代码示例,但我正在使用手机工作,因此无法对其进行测试。
    • 不需要,谷歌回答了。感谢您的帮助!
    猜你喜欢
    • 2015-07-16
    • 2015-08-07
    • 2021-01-30
    • 2020-06-17
    • 2014-02-17
    • 1970-01-01
    • 2016-01-10
    • 2018-05-02
    • 1970-01-01
    相关资源
    最近更新 更多