AJAX 返回错误

AJAX returning an error

本文关键字:错误 返回 AJAX      更新时间:2023-09-26

我正在尝试使用 ajax 填充我的下拉列表,但它返回了一个错误。

这是我的js代码

<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>

这是我的Supplies_controller

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

这是我的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();
}

它执行我在 Java 中的错误函数中的alert("hi");。我似乎没有看到我的代码有任何错误。

顺便说一句,我正在使用代码点火器 3.0。

如果有,请帮我看看。提前谢谢。

         $(data).each(function(){
           $("#category").append($('<option>', {
                value: this.id,
                text: this.category,
            }));
         });