Add data via Codeigniter with Ajax

Asked

Viewed 166 times

1

I’m new with MVC development and so far I’m having difficulty identifying the error when trying to save the records of my form. Give error message: 403 (Forbidden). I am using Codeigniter.

My job:

$(document).ready(function () {
    $('#btn-save').click(function () {
        var dados = $('#form').serialize();
        $.ajax({
            type: 'POST',
            dataType: 'json',
            url: "<?php echo base_url('index.php/centros/cadastrar');?>",
            async: true,
            data: dados,
            success: function (response) {
                location.reload();
            }
        });
        return false;
    });
});
});
  • You notice you have }); more code? is just like that or was it just copy/This is where it came in?

  • Where does this error occur? on the console? If you do location.reload(); in the success of ajax why not do a simple POST without AJAX?

1 answer

0

Little past information. Still, I’ll try to help:

<script>
     $(document).ready(function() {
        $("#botao").click(function(e){
            e.preventDefault();
            var dados   = $('#formulario').serialize();
            $.ajax({
                type: "POST",
                url: "<?php echo site_url('centros/cadastrar'); ?>",
                data: dados,
                success: function(response){ 
                    // TODO faca algo  
                }, 
                error: function(response){
                    // TODO
                }
            });
        });
    });

</script>
Remember that the site_url('centers/register') should be the path to the controller that will do the insertion.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.