Cronas De Se Cronas De Se - 2 years ago 114
PHP Question

AJAX returning an error

I'm trying to populate my dropdown using ajax but it returns an error.

Here is my js code

<script type="text/javascript">
$(document).ready(function() {

$.ajax({
url: "<?php echo base_url('Supplies_controller/getCategory'); ?>",
dataType: 'json',
success: function(data) {
alert(data);
$(data).each(function(){
$("#category").append($('<option>', {
value: this.id,
text: this.category,
}));
})
},
error: function(errorw) {
alert("hi");
}
});
});
</script>


Here is my Supplies_controller

public function getCategory(){
$categories = $this->supplies_model->getCategory();
echo json_encode($categories);
}


And here is my Supplies_model

function getCategory(){
$this->db->select('id, category');
$this->db->from('category');
$this->db->order_by("category", "asc");
$query = $this->db->get();
return $query->result();
}


It executes the
alert("hi");
that is in my error function in java. I don't seem to see anything wrong about my code.

BTW, I am using Codeigniter 3.0.

If there is please help me see it. Thanks in advance.

Answer Source
         $(data).each(function(){
           $("#category").append($('<option>', {
                value: this.id,
                text: this.category,
            }));
         });
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download