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.
Your error could be in the model. Use chrome dev toolkit to find out what the returned page content is.
HTTP CODE 500means server error, typically due to a syntax problem within PHP somewhere.Also,
In your ajax call you should use
success:anderror:. This would allow you to halt execution if the code throws an error.Why are you calling
backend.php/Are you not usingindex.php??One other way to do this is instead of using
.load()you can simply pass back the html from the controller and then onsuccess: function(data){}append it to the container. This would allow you to monitor whether the ajax call waserror()orsuccess()and act accordingly.