Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/439.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/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
Javascript 注册申报表“;401“未经授权”;但请求得到了确认_Javascript_Angularjs_Node.js_Angular Routing - Fatal编程技术网

Javascript 注册申报表“;401“未经授权”;但请求得到了确认

Javascript 注册申报表“;401“未经授权”;但请求得到了确认,javascript,angularjs,node.js,angular-routing,Javascript,Angularjs,Node.js,Angular Routing,我写了一个注册模块,第一步是用户注册他们的帐户作为请求,管理员将确认请求作为下一步。然而,我不知道为什么我的注册请求部分不工作,状态码返回401 Unauthorized,但确认请求工作正常,即使它们在同一路由器中。这是我的密码 在注册控制器中: $scope.signUp = function(){ var req = { username: $scope.username, password: $scope.password,

我写了一个注册模块,第一步是用户注册他们的帐户作为请求,管理员将确认请求作为下一步。然而,我不知道为什么我的注册请求部分不工作,状态码返回401 Unauthorized,但确认请求工作正常,即使它们在同一路由器中。这是我的密码

在注册控制器中:

$scope.signUp = function(){
    var req = {
        username:       $scope.username, 
        password:       $scope.password, 
        email:          $scope.email,
        firstName:      $scope.user.firstName,
        lastName:       $scope.user.lastName,
        contactNumber:  $scope.user.contactNumber,
        companyName:    $scope.user.companyName
    };

    $http.post('/register/request', req)
    .success(function(response){
        if(response === 'success'){
            tagInfoInstace = $uibModal.open({
                animation: true, 
                templateUrl: '../templates/modal_register_tag.html', 
                windowClass: 'modal-tag',
                resolve: {
                    data: function() {
                        return $scope.data;
                    }
                },
                size: 'sm'
            });

            $timeout(function() {
                $state.go('login');
            }, 2000);
        }
        else if(response.startsWith('failed')){
            console.log(response);
        }
        else if(response.startsWith('exist')){
            response.endsWith('user') ? $scope.usernameExisted = true : $scope.emailExisted = true;
        }
    })
    .error(function(response){
        console.log('post error: ' + response);
    });     
}
$scope.confirmTags = function() {
    var data = {};
    var i = 0;
    for(index in $scope.request_checked){
        if($scope.request_checked[index]){
            data[i] = $scope.requests[index];
            i++;
        }
    }

    $http.post('/register/user', data)
    .success(function(response){
        $scope.nonRequestChecked = true;
        $window.location.reload();
    })
    .error(function(response){
        console.log('register error: ' + response);
    });
}
在我的管理控制器中:

$scope.signUp = function(){
    var req = {
        username:       $scope.username, 
        password:       $scope.password, 
        email:          $scope.email,
        firstName:      $scope.user.firstName,
        lastName:       $scope.user.lastName,
        contactNumber:  $scope.user.contactNumber,
        companyName:    $scope.user.companyName
    };

    $http.post('/register/request', req)
    .success(function(response){
        if(response === 'success'){
            tagInfoInstace = $uibModal.open({
                animation: true, 
                templateUrl: '../templates/modal_register_tag.html', 
                windowClass: 'modal-tag',
                resolve: {
                    data: function() {
                        return $scope.data;
                    }
                },
                size: 'sm'
            });

            $timeout(function() {
                $state.go('login');
            }, 2000);
        }
        else if(response.startsWith('failed')){
            console.log(response);
        }
        else if(response.startsWith('exist')){
            response.endsWith('user') ? $scope.usernameExisted = true : $scope.emailExisted = true;
        }
    })
    .error(function(response){
        console.log('post error: ' + response);
    });     
}
$scope.confirmTags = function() {
    var data = {};
    var i = 0;
    for(index in $scope.request_checked){
        if($scope.request_checked[index]){
            data[i] = $scope.requests[index];
            i++;
        }
    }

    $http.post('/register/user', data)
    .success(function(response){
        $scope.nonRequestChecked = true;
        $window.location.reload();
    })
    .error(function(response){
        console.log('register error: ' + response);
    });
}
这是路由器:

router.post('/register/:type', function(req, res, next){
if(req.params.type === 'request'){
    var request_user = new Request({
        username:       req.body.username,
        password:       req.body.password,
        email:          req.body.email,
        firstName:      req.body.firstName,
        lastName:       req.body.lastName,
        contactNumber:  req.body.contactNumber,
        companyName:    req.body.companyName
    });

    request_user.save(function(err) {
        if(err)
            res.send('failed: save error: ' + err);
        else
            res.send('success');
    });
}
else if(req.params.type === 'user'){
    var exception = {};
    for(index in req.body){
        var user = new User({
            username:       req.body[index].username,
            password:       req.body[index].password,
            email:          req.body[index].email,
            firstName:      req.body[index].firstName,
            lastName:       req.body[index].lastName,
            contactNumber:  req.body[index].contactNumber,
            companyName:    req.body[index].companyName
        });

        user.save(function(err) {
            if(err)
                exception[index] = 'failed: save error ' + index + ': ' + err;
        });

        Request.find().remove({username: req.body[index].username}).exec(function(err, feedback){
            if(err)
                exception[index*2] = 'delete error: ' + index + ': ' + err;
        });
    }
    res.send(exception);
}

}))

console.dir(req.body)
显示了什么,它是正确填写的还是空的?另外,在异步回调(所有)完成之前,您正在调用
res.send(exception)
,这意味着
/register/user
将始终发回空对象。路由器拒绝了post,错误代码401未经授权,感谢您解决了异步回调的问题听起来可能有一个身份验证中间件在已经用401响应之后将执行(例如,通过
next()
)传递给路由处理程序?哦,您是对的,我忘了一开始我有一个访问控制中间件。修好了,谢谢!