2016-02-07 174 views
-1

我想使用ajax填充我的下拉菜单,但它返回一个错误。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(); 
} 

它执行alert("hi");这是我的错误功能java的。我的代码似乎没有看到任何错误。

顺便说一句,我正在使用Codeigniter 3.0。

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

+0

用'错误更换你的错误部分:功能(X,Y,Z){执行console.log(X + “” + Y + “” + Z)}' 并检查什么错误AJAX抛出。 .. –

+0

是的请更新您的问题与AJAX错误 –

+2

@AmarShukla - '错误:funciton'?拼写错误! – RamRaider

回答

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