Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs Angular未从django rest框架接收401和400 http响应_Angularjs_Django_Django Rest Framework - Fatal编程技术网

Angularjs Angular未从django rest框架接收401和400 http响应

Angularjs Angular未从django rest框架接收401和400 http响应,angularjs,django,django-rest-framework,Angularjs,Django,Django Rest Framework,当我发送正确的凭证时,我会得到HTTP_200_OK响应,一切都按预期进行。但是,当我发送不正确或不完整的登录凭据时,我不会得到任何响应。这是我的密码: 角度(authService.js登录函数): Django rest框架-登录视图: “400”和“401”分别打印在控制台中,但我没有得到任何响应。帮忙 编辑 谢谢你,明白了。在错误回调函数中接收错误响应: $http({ method : 'POST', url : API_LOCATION + 'login',

当我发送正确的凭证时,我会得到HTTP_200_OK响应,一切都按预期进行。但是,当我发送不正确或不完整的登录凭据时,我不会得到任何响应。这是我的密码:

角度(authService.js登录函数):

Django rest框架-登录视图:

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

编辑

谢谢你,明白了。在错误回调函数中接收错误响应:

$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)
});

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

更新:

$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;
});

你能给我举个例子吗?我添加了一个快速的代码示例,但我在手机上工作,所以我无法测试它。没必要,谷歌回答了。谢谢你的帮助!
$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)
});
$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;
});