PHP User
PHP User

Reputation: 2422

codeigniter ajax post 'internal server error' on localhost

[UPDATE]: in config.php: $config['base_url'] = 'http://localhost/posts/';

I'm using the controller file ajax_controller.php to handle the ajax posts in a view page with this code

$(document).on('click','a.delete',function (e) {
        e.preventDefault();

        var id = $(this).attr('id');
        noty({
            text        : 'post will be deleted',
            type        : 'alert',
            dismissQueue: true,
            layout      : 'center',
            theme       : 'defaultTheme',
            modal       : true,
            buttons     : [
                {addClass: 'btn btn-primary', text: 'Ok', onClick: function ($noty) {

                    $.ajax({
                        type: "POST",
                        url: "<?=base_url()?>" + "ajax_controller/del_post",
                        data: {id: id},
                        dataType: "text",
                        cache:false,
                        success:
                            function(data){
                                //alert(data);
                                $noty.close();
                                noty({dismissQueue: true, force: true, layout: 'center', theme: 'defaultTheme', text: 'You clicked "OK" button', type: 'success',timeout:'2000'});
                            }
                    });
                }
                },
                {addClass: 'btn btn-danger', text: 'Cancel', onClick: function ($noty) {
                    $noty.close();
                    noty({dismissQueue: true, force: true, layout: 'center', theme: 'defaultTheme', text: 'You clicked "Cancel" button', type: 'error',timeout:'2000'});
                }
                }
            ]
        });
    });

The controller contains this code

class ajax_controller extends CI_Controller{

function __construct()
{
    parent::__construct();
    $this->load->model('posts_model');
}

function del_post($postID){
    $this->posts_model->del_post($postID);
    echo 'success';
}
}

the posts_model contains this function (and other functions working fine)

function del_post($postID){
    $this->db->delete()->from('posts')->where('Post_ID',$postID);
}

But when I click the delete button I get this error

http://[::1]/posts/ajax_controller/del_post 500 (Internal Server Error)

I've changed the url to

url: "<?=base_url()?>" + "ajax_controller/del_post/"+id,

and commented data: {id: id}, but I get the same error. So my question is how to properly create a post ajax in codegniter by editing this ajax.

Upvotes: 1

Views: 4575

Answers (1)

Zeeshan
Zeeshan

Reputation: 801

As per your post the requested URL is wrong as if it's on localhost it should be like : http://127.0.0.1/posts/ajax_controller/del_post

or

http://localhost/posts/ajax_controller/del_post

Try setting your base_url in config.php and it should fix your issue.

Let me know your queries.

----EDIT------

You have written wrong delete query,it should go like this

$this->db->delete('mytable', array('id' => $id)); 

Refer this link for more info https://www.codeigniter.com/userguide2/database/active_record.html#delete

Upvotes: 3

Related Questions