Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery AJAX returning error 500 despite the successful execution on server side

I am encountering a problem with codeigniter and JQuery Ajax Post.

My javscript

        $('.remove').click(function(){
        var category=event.target.id;
        var id=$('input[name=article_id]').val();
        var p={};
        p['id']=id;

        $.ajax({
            type: "POST",
            url: "/backend.php/blog/removeCategories",
            async:true,
            cache:false,
            data: {id: id, category: category }
            }).done(function(msg){
                jQuery('#category_list').load('/backend.php/blog/refreshCategories/',p,function(str){});
            });

My codeigniter's controller

 function removeCategories(){
        $id=$_POST['id'];
        $category_id=$_POST['category'];

        $this->article->removeCategory($category_id,$id);
    }

I can't get the ajax function to work because there is always an error 500 received from the server. Although, firebug returns that there is an error loading the resources, the function removeCategories was executed anyways.

like image 827
Mark Seah Avatar asked May 11 '12 13:05

Mark Seah


People also ask

What is 500 Internal server error Ajax?

The 500 Internal Server Error is a very general HTTP status code. It means something has gone wrong on the website and webserver is unable to specify what exactly, thus failing in fulfilling the request made by the client.

Why do I get 500 internal server error?

The 500 Internal Server error could be caused by an error during the execution of any policy within Edge or by an error on the target/backend server. The HTTP status code 500 is a generic error response. It means that the server encountered an unexpected condition that prevented it from fulfilling the request.


1 Answers

Make sure your data is being passed properly by making the following changes to the data option.

$.ajax({
            type: "POST",
            url: "/backend.php/blog/removeCategories",
            async:true,
            cache:false,
            data: {"id": id, "category": category }
            }).done(function(msg){
                jQuery('#category_list').load('/backend.php/blog/refreshCategories/',p,function(str){});
            });

The way you have it coded, the key of each key value pair is being set to the variable's value.

like image 89
marteljn Avatar answered Oct 16 '22 11:10

marteljn