We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Difficulty using tokens with Ajax

Sorry to bother you, but for several days I have tried all the methods that the security token work with AJAX I do not know if I am, but do not use AJAX when the token is working properly. It might in some way, a method was found and included in the documentation as this has so far been a great challenge to find this solution.

in view volt:

 <form id="register-form" role="form" style="display: none;">
                            <div class="form-group">
                                <input type="text" name="user_username" id="user_username" tabindex="1" class="form-control" placeholder="{{ _('Nombre de Usuario') }}" value="">
                            </div>
                            <div class="form-group">
                                <input type="text" name="user_firstname" id="user_firstname" tabindex="1" class="form-control" placeholder="{{ _('Ingrese su Nombre') }}" value="">
                            </div>
                            <div class="form-group">
                                <input type="text" name="user_lastname" id="user_lastname" tabindex="1" class="form-control" placeholder="{{ _('Ingrese su Apellido') }}" value="">
                            </div>
                            <div class="form-group">
                                <div class="input-group">
                                    <span class="input-group-addon">@</span>
                                    <input type="email" name="user_email" id="user_email" tabindex="1" class="form-control" placeholder="{{ _('Ingrese su email') }}" value="">
                                </div>
                            </div>
                            <div class="form-group">
                                <div class="input-group">
                                    <span class="input-group-addon">#</span>
                                    <input type="number" name="user_phone" id="user_phone" tabindex="1" class="form-control" placeholder="{{ _('Ingrese su Telefono') }}" value="">
                                </div>
                            </div>
                            <div class="form-group">
                                <input type="password" name="user_password" id="user_password" tabindex="2" class="form-control" placeholder="{{ _('Ingrese su password') }}">
                            </div>
                            <div class="form-group">
                                <input type="password" name="user_confirm_password" id="user_confirm_password" tabindex="2" class="form-control" placeholder="{{ _('Confirme su password') }}">
                            </div>
                            <div class="form-group">
                                <div class="row">
                                    <div class="col-sm-6 col-sm-offset-3">
                                        <input type="submit" name="register-submit" id="register-submit" tabindex="4" class="form-control btn btn-register" value="{{ _('Registrarse') }}">
                                    </div>
                                </div>
                            </div>
                            <input type="hidden" class="gtoken" name="" value=""/> // security token change every request of ajax
                            <div id="message_info_register"></div>
                        </form>

in controller:

 public function getTockenAction() {
    $this->view->disable();
    $token = array('tokenKey' => $this->security->getTokenKey(), 'token' => $this->security->getToken());
    $a_response = $this->a_bts_json_response;
    $a_response['data'] = $token; // this must be return to ajax request
    $this->_fnSYSResponseFormat($a_response, self::BTS_RESPONSE_TYPE_JSONP); // is equal to: json_encode();
}

js :

fngetTokenPublic($('.gtoken'));
    function fngetTokenPublic(form) {
    $.ajax({
        url: '/public/general/gettocken.json'
        , data: {}
        , success: function (j_response) {
            $(form).attr('name', j_response.data.tokenKey);
            $(form).attr('value', j_response.data.token);
        }
    });
}

and for every ajax request new token was generate