更改页面后,Recaptcha无法获得角度响应

时间:2016-12-30 14:20:16

标签: angularjs forms recaptcha

我的角度应用程序有问题。我的网站上有一个注册页面。通常,当我直接进入注册页面时,它可以正常工作,在提交表单后发送并注册用户。当我例如加载注册页面然后转到登录页面然后再次注册时出现问题。在这种情况下,表单不会发送到服务器。
我试图解决这个问题,甚至在点击注册链接后刷新页面进行修复,但没有帮助。

我调试了一下我的应用程序,发现它是重新调用导致我的问题。我使用angular-recaptcha 2.2.5版;试图记录vcRecaptchaService.getResponse()的输出,但在控制台中没有显示。

以下是一些代码,问题可能在于:

表格要求

$scope.registerRequest = (form) => {
    $scope.$broadcast('show-errors-check-validity');
    if (!form.$valid) {
        return;
    }

    $scope.isLoading = true;
    $scope.formData.reCaptcha = vcRecaptchaService.getResponse();

    apiRequest.post('user/register', $scope.formData).success((response) => {
        $scope.isLoading = false;
        $scope.registered = true;
        $scope.formData = {};
    });
};


路线:

app.config(['$routeProvider', ($routeProvider) => {
$routeProvider
        .when('/auth/login', {
            controller: 'authLogin',
            label: 'Logowanie',
            templateUrl: 'app/components/authLoginView.html',
            access: ['UNAUTH']
        })
        .when('/auth/register/', {
            controller: 'authRegister',
            label: 'Rejestracja',
            templateUrl: 'app/components/authRegisterView.html',
            access: ['UNAUTH']
        })
        .when('/auth/register/confirm', {
            controller: 'authRegister',
            label: 'Potwierdzenie rejestracji',
            templateUrl: 'app/components/authRegisterView.html',
            access: ['UNAUTH']
        })
        .when('/auth/register/resend', {
            controller: 'authRegister',
            label: 'Rejestracja',
            templateUrl: 'app/components/authRegisterView.html',
            access: ['UNAUTH']
        })
}]);


还有一些HTML:

<div ng-if="section == 'register'" class="container employer-container">
    <form name="registerForm" class="form-horizontal col-sm-6 col-sm-offset-3" loader is-loading="isLoading">
        <h4 class="employer-h4">Rejestracja</h4>

        <p class="bg-success text-success col-xs-12" ng-show="registered">
            Użytkownik został zarejestrowany. Na podany adres e-mail wysłaliśmy dalsze instrukcje.
        </p>

        <div ng-hide="registered">
            <div class="form-group" show-errors>
                <label for="email" class="col-md-3 control-label">E-mail:</label>
                <div class="col-md-9">
                    <input type="text" class="form-control" id="email" placeholder="E-mail"
                           ng-model="formData.email" name="username"
                           ng-required="true">
                </div>
            </div>
            <div class="form-group" show-errors>
                <label for="password" class="col-md-3 control-label">Hasło:</label>
                <div class="col-md-9">
                    <input type="password" class="form-control" id="password" placeholder="Hasło"
                           ng-model="formData.password" name="password" ng-minlength="5"
                           ng-required="true" equals="{{ formData.confirmPassword }}">
                </div>
            </div>
            <div class="form-group" show-errors>
                <label for="confirmPassword" class="col-md-3 control-label">Powtórz hasło:</label>
                <div class="col-md-9">
                    <input type="password" class="form-control" id="confirmPassword" placeholder="Powtórz hasło"
                           ng-model="formData.confirmPassword" name="confirmPassword" ng-minlength="5"
                           ng-required="true" equals="{{ formData.password }}">
                </div>
            </div>
            <div class="form-group" show-errors>
                <label class="col-md-3 control-label" for="userType">Rodzaj konta:</label>
                <div class="col-md-9">
                    <div class="btn-group" dropdown>
                        <button type="button" class="btn btn-default dropdown-toggle form-control"
                                id="userType" name="userType" dropdown-toggle ng-model="formData.userType"
                                ng-required="true">
                            {{ userTypes[formData.userType] || 'rodzaj konta' }} <span class="caret"></span>
                        </button>
                        <ul class="dropdown-menu" role="menu">
                            <li ng-repeat="(key, userType) in userTypes">
                                <a href="" ng-click="$parent.formData.userType = key">{{ ::userType }}</a>
                            </li>
                        </ul>
                    </div>
                </div>
            </div>
            <div class="form-group" show-errors>
                <div class="col-md-3"></div>
                <div class="col-md-9">
                    <input class="form-control" type="checkbox" id="acceptTerms" ng-model="formData.acceptedTerms" name="acceptTerms" ng-required="true">
                    <label class="control-label" style="text-align: left;" for="acceptTerms">Zgadzam się z  <a href="/#!/page/4" style="color: #3C5B9B;">Regulaminem</a></label>
                </div>
            </div>
            <div class="form-group" show-errors>
                <div class="col-md-3"></div>
                <div class="col-md-9">
                    <input class="form-control" type="checkbox" id="acceptTerms2" ng-model="formData.acceptedTerms2" name="acceptTerms2" ng-required="true">
                    <label class="control-label" style="text-align: left;" for="acceptTerms2">Wyrażam zgodę na przetwarzanie moich danych w celu realizacji usług w ramach Serwisu i akceptuję <a href="/#!/page/5" style="color: #3C5B9B;">Politykę Prywatności.</a>.</label>
                </div>
            </div>
            <div class="form-group" show-errors>
                <div class="col-md-3"></div>
                <div class="col-md-9">
                    <input class="form-control" type="checkbox" id="acceptTerms3" ng-model="formData.acceptedTerms3" name="acceptTerms3" ng-required="true">
                    <label class="control-label" style="text-align: left;" for="acceptTerms3">Wyrażam zgodę na przetwarzanie moich danych w <a href="/#!/page/9" style="color: #3C5B9B;">celach marketingowych.</a></label>
                </div>
            </div>
            <div class="form-group">
                <div class="col-md-9 col-md-offset-3">
                    <div vc-recaptcha key="'key'"></div>
                </div>
            </div>
            <div class="form-group">
                <div class="col-md-6 col-md-offset-3">
                    <a href="#!/auth/lostpassword">Zapomniane hasło</a> |
                    <a href="#!/auth/login">Logowanie</a>
                </div>
                <div class="col-md-3 text-right">
                    <button type="submit" class="btn btn-info" ng-click="registerRequest(registerForm)">Zarejestruj</button>
                </div>
            </div>
        </div>
    </form>
</div>

问题可以在这里看到:http://pze2.biuro.netivo.pl/

在我的路线中回答关于['UNAUTH']的一个问题。它仅允许未登录的用户进入此页面。

1 个答案:

答案 0 :(得分:2)

感谢Vinny,我设法解决了这个问题。

问题在于他在reCaptcha.getResponse()中说没有得到正确的小部件。

对于那些有同样问题的人,我把解决方案放在我的代码中:

请求:

$scope.registerRequest = (form) => {
    $scope.$broadcast('show-errors-check-validity');
    if (!form.$valid) {
        return;
    }

    $scope.isLoading = true;

    apiRequest.post('user/register', $scope.formData).success((response) => {
        $scope.isLoading = false;
        $scope.registered = true;
        $scope.formData = {};
    });
};

HTML:

<div ng-if="section == 'register'" class="container employer-container">
    <form name="registerForm" class="form-horizontal col-sm-6 col-sm-offset-3" loader is-loading="isLoading">
        <h4 class="employer-h4">Rejestracja</h4>

        <p class="bg-success text-success col-xs-12" ng-show="registered">
            Użytkownik został zarejestrowany. Na podany adres e-mail wysłaliśmy dalsze instrukcje.
        </p>
        <div ng-hide="registered">
            ...
            <div class="form-group">
                <div class="col-md-9 col-md-offset-3">
                    <div vc-recaptcha ng-model="formData.reCaptcha" key="'key'"></div>
                </div>
            </div>
            <div class="form-group">
                <div class="col-md-6 col-md-offset-3">
                    <a href="#!/auth/lostpassword">Zapomniane hasło</a> |
                    <a href="#!/auth/login">Logowanie</a>
                </div>
                <div class="col-md-3 text-right">
                    <button type="submit" class="btn btn-info" ng-click="registerRequest(registerForm)">Zarejestruj</button>
                </div>
            </div>
        </div>
    </form>
</div>