0

I have a pretty simple CodeIgniter project that does some simple database work. Currently I have a controller method that deletes an item in the database. In my view, the code uses the jQuery ajax object to reference that controller, then displays either success or failure through its callbacks.

The problem is, although the controller works fine (deletes the item in the DB as expected), the ajax "error" callback is being called, instead of "success".

I'm sure I'm missing something super basic here...

Here is my controller method:

public function delete_note()
{
    $this->load->helper('url');
    $this->load->model('auth/user', 'User');
    $this->load->database();

    $note_id = $this->input->post('id');

    $this->db->delete('admin_notes', array('id' => $note_id));
    $this->db->query();

    $this->output->set_status_header('200'); // Attempting to force the issue
}

And here is the code within the view:

$('.delete_note').each(function(){
    var current = this;

    this.onclick = function(event)
    {
        var note_id = this.title;

        $.ajax({
            type: "POST",
            url: "/admin/delete_note",
            data: { id: note_id },

            success: function(data) { alert('I never see this!');},
            error: function () { alert('I *always* see this!');}
        });
    }
});

Any answer attempts are appreciated. Any correct answers are doubly appreciated ;)

Thanks!

2
  • 3
    Have you tried to use firebug or chrome developer tools to see what network response are you getting? Commented May 28, 2012 at 20:33
  • Try turning on logging in codeigniter to see what the issue could be. Commented May 28, 2012 at 20:35

2 Answers 2

3

Just because the database logic is working, you may be receiving issues related to something else (cross-browser perhaps?). To see the error, change your error function to be like this:

error: function (jqXHR, textStatus, errorThrown) {
   alert(textStatus);
   alert(errorThrown);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for that. I'll alter my error functions to include this important information for future potential problems. Much appreciated.
0

I'd like to thank WojtekT for reminding me to check my responses in Firebug. I was actually receiving back a 500 status code for PHP errors. My extraneous $this->db->query(); call needed to be removed.

Everything works great now.

Thanks!

1 Comment

Also, to point out, the query function takes an argument (of a string query, of course), which is where the error was originating from.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.