Welcome Guest, Not a member yet? Register   Sign In
Codeigniter and Angular 1 Request Help
#1

Hi, im doing one application that envolves CI and Angular, thats my first time using those two together.

Theres a ways of using the same controller function in CI to return ajax or return view?
This is what i have by now:

Code:
public function registrar()
    {
        if($this->input->is_ajax_request()){
            $coco = array();
            $coco['Teste'] = "Teste";
            $this->output->set_content_type('application/json');
            $this->output->set_output(json_encode($coco));
        }else{
            $this->lib_templates->set_title('Registre-se.');
            $this->lib_templates->set_js('js/controllers/usuariosController.js', 'footerBottom', 'local');
            $this->lib_templates->set_template('templates/main');
            $this->lib_templates->set_view('usuarios/registrar');
            $this->lib_templates->render();
        }
    }

In angular:

Code:
console.log(moment().format("YYYY-MM-DD"));
    $scope.registrar = function(){
        Loading.show();
        console.log("REGISTRAR");
        console.log($scope.usuario);
        $http({
            method: 'POST',
            url: baseUrl + 'usuarios/registrar',
            data: {
                email: $scope.usuario.iptEmail,
                senha: $scope.usuario.iptSenha,
                tp_usuario: 3,
                dt_cadastro:  moment().format("YYYY-MM-DD")
            },
            headers : {'Content-Type': 'application/x-www-form-urlencoded'}
        }).then(function successCallback(response) {
            console.log(response, "success");
            Loading.hide();
        }, function errorCallback(response) {
            console.log(response, "error");
            Loading.hide();
        });
    }

This is not working now, but i how i wanted to be... anyone help please?
Reply
#2

Make sure you add something like this to your html head section:

Code:
<script>
   var baseUrl = "<?php echo base_url(); ?>";
   var siteUrl = "<?php echo site_url(); ?>";
   // Extra variables for Javascript.
   var currentGroupId = <?php echo $group_id; ?>;
</script>

This will give your javascript code CI's base and site url's to it, also shows how to pass variables to it.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

Actually i found what i'v looking for...
First i need to implemente some $httpProvider to angular:
Code:
dgApp.config(['$httpProvider', function($httpProvider) {
    $httpProvider.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";
    $httpProvider.defaults.headers.common["X-Requested-With"] = "XMLHttpRequest";
    // Solução para os objetos aparecerem no $_POST, ou isso ou $httpParamSerializer direto no $http
    $httpProvider.interceptors.push(['$q', function($q) {
        return {
            request: function(config) {
                if (config.data && typeof config.data === 'object') {
                    config.data = $.param(config.data);
                }
                return config || $q.when(config);
            }
        };
    }]);
}]);
Doing this to your angular app u can check in CI controller for ajax request like:

Code:
if($this->input->is_ajax_request()){
//some code
}

And applying those configurations to angular app, you can access the CI input ($this->input->post()) and retrieve everything that has been sent through the ajax!
(Other way is doing those first 2 configs to httpProvider and using httpParamSerializer angular option)
Hope this can help someone in the furute.
Sorry for my english, is not that good since is not my native language...
Cheers!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB