Reputation: 331
I am trying to get data from my database using ajax call but it returns this error:
This is my AJAX call
$.ajax({
url : '<?php echo base_url('Create_controller/getCategory'); ?>',
dataType : 'json',
success: function(data) {
$(data).each(function(){
$('#create_category').append($('<option>', {
value: this.id,
text: this.category,
}));
})
},
error: function(errorw) {
alert("error");
}
});
This is my Create_controller:
public function getCategory(){
$categories = $this->create_model->getCategory();
echo json_encode($categories);
}
This is my Create_model:
function getCategory(){
$this->db->select('id, category');
$this->db->from('category');
$this->db->where('status', 1);
$this->db->order_by('category', 'asc');
$query = $this->db->get();
return $query->result();
}
I know that my controller and model works because I have tried using print_r($this->create_model->getCategory());
before loading the view.
I have been searching for 3 hours now but none of them solves my problem.
Thanks for the help.
Upvotes: 1
Views: 1445
Reputation: 331
The cause of this problem is that I forgot to add my .htaccess
file.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Upvotes: 0
Reputation: 24
Just check that you are posting data in a post method or get method. Try this function to call Ajax:
var request = $.ajax({
url: "yourURl",
type: "POST",
dataType: "json"
});
Upvotes: 1
Reputation: 5432
This is temporary but replace
'<?php echo base_url('Create_controller/getCategory'); ?>'
with
"<?php echo base_url('Create_controller/getCategory'); ?>"
and say if it works...
Upvotes: 0
Reputation: 3962
Your controller doesn't exist where you expect it. Right-click on to the URL in the error message and try "open in new tab" if you're in Chrome. This has nothing to do with Ajax. It's related to your MVC setup and routing.
Upvotes: 0