CodeIgniter中Ajax搜索实现问题:请求404错误排查与解决
解决CodeIgniter Ajax搜索的404错误及功能修复
先帮你拆解下问题根源,再一步步修复:
一、404错误的核心原因
你的sent控制器方法存在几个致命问题:语法错误导致方法执行中断,且没有向Ajax请求返回任何内容,同时Model和前端请求也有细节错误,最终触发了404或无效响应。
二、分步修复方案
1. 修复Controller的语法与响应逻辑
首先修正invoice_invoice.php中的错误,确保方法能正常执行并返回内容:
<?php function sent() { $limit_per_page = 10; $search_term = $this->input->get('search'); // 把参数名改成更语义化的search $page = ($this->uri->segment(3)) ? ($this->uri->segment(3) - 1) : 0; // 处理搜索/默认列表逻辑 if (!is_null($search_term) && trim($search_term) !== '') { $total_records = $this->invoice_model->get_total_search($search_term); $data['content'] = $this->invoice_model->search($search_term, $limit_per_page, $page * $limit_per_page); } else { // 无搜索词时加载默认已发送列表(需在Model实现对应方法) $total_records = $this->invoice_model->get_total_sent(); $data['content'] = $this->invoice_model->get_sent_invoices($limit_per_page, $page * $limit_per_page); } // 修复分页标签的错误(之前num_tag_close写错了) $config['base_url'] = site_url('invoice_invoice/sent'); $config['total_rows'] = $total_records; $config['per_page'] = $limit_per_page; $config["uri_segment"] = 3; $config['num_links'] = 2; $config['use_page_numbers'] = TRUE; $config['reuse_query_string'] = TRUE; $config['full_tag_open'] = '<div class="pagination dataTables_paginate paging_bootstrap_number" id="sample_3_paginate"> <ul class = "pagination" style = "visibility : visible;">'; $config['full_tag_close'] = '</ul></div>'; $config['next_link'] = '<i class="fa fa-angle-right"></i>'; $config['next_tag_open'] = '<li class = "next">'; $config['next_tag_close'] = '</li>'; $config['prev_link'] = '<i class="fa fa-angle-left"></i>'; $config['prev_tag_open'] = '<li class = "prev">'; $config['prev_tag_close'] = '</li>'; $config['cur_tag_open'] = '<li class = "active"><a>'; $config['cur_tag_close'] = '</a></li>'; $config['num_tag_open'] = '<li>'; $config['num_tag_close'] = '</li>'; $this->pagination->initialize($config); $data["links"] = $this->pagination->create_links(); // 关键:区分Ajax请求和普通请求,返回对应内容 if ($this->input->is_ajax_request()) { // 加载仅包含结果的局部视图(需新建这个视图文件) $this->load->view('invoice_sent_results', $data); } else { // 普通请求加载完整页面 $this->load->view('invoice_sent', $data); } } ?>
2. 修复Model的Search方法
你的select语句错误地把字段用单引号包裹,导致SQL无法识别正确字段,同时遗漏了分页的limit参数:
function search($search,$limit,$start) { $userdata1 = $this->session->userdata('condition'); // 去掉字段外的单引号,正确指定要查询的字段 $this->db->select("id, Client_Name, ac_email, Client_email, Invoice_no, Items, client_add, Currency, Language, Notes"); $whereCondition = array( 'Client_email' => $search, 'ac_email' => $userdata1['ac_email'] ); $this->db->where($whereCondition); $this->db->from('invoices'); $this->db->limit($limit, $start); // 添加分页限制 $params = $this->db->get(); return $params->result(); }
同时确保get_total_search方法正确统计总记录数:
function get_total_search($search) { $userdata1 = $this->session->userdata('condition'); $whereCondition = array( 'Client_email' => $search, 'ac_email' => $userdata1['ac_email'] ); $this->db->where($whereCondition); return $this->db->count_all_results('invoices'); }
3. 优化jQuery Ajax请求
调整参数传递方式,增加错误处理,同时处理搜索框清空的场景:
$(document).ready(function () { $("#search").keyup(function () { var str = $(this).val().trim(); var $resultContainer = $("#sample_1"); if (str !== "") { console.log("搜索关键词:", str); $.get("<?php echo base_url('invoice_invoice/sent');?>", {search: str}) .done(function (data) { $resultContainer.html(data); }) .fail(function(xhr, status, error) { console.error("请求失败:", status, error); $resultContainer.html('<div class="alert alert-danger">搜索失败,请稍后重试</div>'); }); } else { // 清空搜索框时加载默认列表 $.get("<?php echo base_url('invoice_invoice/sent');?>") .done(function(data) { $resultContainer.html(data); }); } }); });
4. 调整视图结构
新建一个局部视图invoice_sent_results.php,专门存放搜索结果的表格和分页,这样Ajax请求返回后可以直接替换页面内容:
<table id="sample_1" class="table table-striped table-bordered"> <thead> <tr> <th>ID</th> <th>客户名称</th> <th>客户邮箱</th> <!-- 其他表头字段 --> </tr> </thead> <tbody> <?php foreach($content as $item): ?> <tr> <td><?php echo $item->id; ?></td> <td><?php echo $item->Client_Name; ?></td> <td><?php echo $item->Client_email; ?></td> <!-- 其他字段内容 --> </tr> <?php endforeach; ?> </tbody> </table> <!-- 分页链接 --> <?php echo $links; ?>
三、额外检查点
- 确认Controller文件名是
Invoice_invoice.php(CodeIgniter要求控制器文件名首字母大写) - 查看
application/logs/下的日志文件,排查是否有隐藏的语法错误 - 确保路由配置没有覆盖
invoice_invoice/sent的默认路由
内容的提问来源于stack exchange,提问作者Bhargav Trivedi
相关产品推荐
相关产品推荐

