我得到[500]内部服务器错误此代码试图在Codeigniter中实现自动完成。
http://localhost/pier_capitan/index.php/commissary/autocomplete_items?term=a
以下是我的代码。
查看
<div class="row">
<div class="col-md-12" style="margin: 0 auto; margin-top: 10px;">
<div class="panel panel-primary">
<div class="panel-heading"><h3>Add New Inventory</h3></div>
<div class="panel-body">
<form class="add_new_inventory form-inline" role="form" id="add_new_inventory" method="post">
<div class="form-group form-control">
<label>Date</label>
<input type="date" name="date_added" required>
</div>
<div class="btn btn-warning pull-right" onclick="add_new_row()">Add more</div>
<hr>
<div style="font-weight: bold;">Total Php <input type="text" id="total_result" placeholder="0.00" class="form-control" readonly></div>
<br>
<table class="table table-striped table-bordered table-condensed" id="add_new_inventory">
<colgroup>
<col span="1" style="width: 20%;">
<col span="1" style="width: 10%;">
<col span="1" style="width: 10%;">
<col span="1" style="width: 10%;">
<col span="1" style="width: 5%;">
</colgroup>
<thead>
<tr>
<th class="ui-helper-center">Item Name
</th>
<th class="ui-helper-center">Quantity</th>
<th class="ui-helper-center">Amount</th>
</tr>
</thead>
<tbody>
<tr>
<td width="50%">
<input type="text" id="item_name" name="item_name[]" placeholder="Item Name" class="form-control input-sm" style="width: 100%;">
</td>
<td width="10%">
<input type="text" name="quantity[]" placeholder="quantity" class="form-control input-sm">
</td>
<td width="10%">
<input type="number" name="amount[]" placeholder="Amount" class="form-control input-sm">
</td>
</tr>
</tbody>
</table>
</form>
</div><!-- panel body -->
</div><!-- panel -->
</div><!-- col-md-12 -->
</div><!-- row -->
</div><!-- container -->
</body>
<script type="text/javascript" src="<?php echo base_url('assets/jquery/jquery.js');?>"></script>
<script type="text/javascript" src="<?php echo base_url('assets/bootstrap/js/bootstrap.min.js');?>"></script>
<script type="text/javascript" src="<?php echo base_url('assets/datatables/js/jquery.dataTables.min.js');?>"></script>
<script type="text/javascript" src="<?php echo base_url('assets/datatables/js/dataTables.bootstrap.js');?>"></script>
<script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js" ></script>
<script type="text/javascript">
$(document).ready(function(){
var url;
$('input[name=date_added]').val('<? echo date('Y-m-d')?>');
$('#item_name').autocomplete({
source:function(request, response){
url="<?php echo site_url('commissary/autocomplete_items');
?>";
$.ajax({
url:url,
datatype:"json",
data:request,
type:"POST",
success:function(data){
response(data.items);
}
});
}
});
});
CONTROLLER
public function autocomplete_items(){
return $this->items_model->autocomplete_items($this->input->post('term'));
MODEL
public function autocomplete_items($item){
$item=trim($item);
$this->db->select('item_name');
$this->db->from('inv_itms');
$this->db->like('item_name',$item);
$this->db->limit('5');
$query=$this->db->get();
if($query->num_rows()>0){
$data['items']=array();
foreach($query->result() as $row){
$data['items'][]=array(
'label'=>$row->item_name,
'value'=>$row->item_name
);
}
}
echo json_encode($data);
}
在输入&#34; item_name&#34;,当我输入一个字母时,我应该得到结果,但相反,我得到500服务器错误。你能帮我吗。谢谢。
答案 0 :(得分:1)
您的问题是传递给自动完成source的请求参数是一个具有单个属性术语的对象,因此如果您在输入中键入foo
,请求对象将为{{1} }。你有2个选择
由于您已经将完整的请求对象发送到服务器,因此只需在控制器中使用它
即可{term:'foo'}
或者,在您的ajax调用中,使用您的自定义对象发送关键字
public function autocomplete_items(){
return $this->items_model->autocomplete_items($this->input->post('term'));
}
请注意我还添加了类型:&#34; POST&#34;到ajax,也应该使用第一个选项。
答案 1 :(得分:0)
控制器更改
public function autocomplete_items(){
$data = $this->model->...;
echo $data;
}
答案 2 :(得分:0)
您正在使用ajax post方法将数据从视图发送到控制器,并尝试通过
获取 $this->input->post('term')
您无法使用此功能接收变量。
505错误主要有两个原因: