【问题标题】:Why am I getting no response from ajax data in codeigniter 3?为什么我在 codeigniter 3 中没有收到来自 ajax 数据的响应?
【发布时间】:2021-08-16 23:09:25
【问题描述】:

我正在搜索数据而不刷新页面,但我没有收到任何请求响应。我尝试编辑我的 ajax 代码,但没有任何反应。我昨天刚开始学习 ajax,所以它对我来说是新的。它在我的 ajax 代码上还是在我的控制器上?我什至包含了我的 ajax 链接。

这是我的视图代码:

<html>
<head>
    <meta charset="utf-8">
    <title>CRUD Practice</title>
    <style type="text/css">
        *{
            font-family: helvetica;
        }
        a{
            text-decoration: none;
            margin-top: 50px;
        }
    </style>
    <script
  src="https://code.jquery.com/jquery-3.6.0.min.js"
  integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4="
  crossorigin="anonymous"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

</head>
<body>
    <h2>Search: <input type="text" name="search_text" placeholder="Enter your query" id="search_text"></h2>

    <h1>Admin Dashboard</h1>
    <a href="get_all">Get all data</a><br>
    <a href="get_id/(:num)">Get data by ID</a><br>
    <a href="add_view">Add data</a>
    <div id="result"></div>

<a href="<? echo base_url() ?>">Go Back</a>

<script>
        $(document).ready(function(){
            
            load_data();
            function load_data(query){
                $.ajax({
                    url:"<?php echo  base_url()?>crud_controller/fetch",
                    method:"POST",
                    data : {query: query},
                    success: function(data){
                            $('#result').html(data);
                    }
                })
            }

            $('#search_text').keyup(function(){
                var search = $(this).val();
                if(search != " "){
                        load_data(search);
                }else{
                        load_data();
                }
            })
        })
</script>
</body>
</html>

这是我的控制器:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Crud_controller extends CI_Controller {
    public function __construct(){
        parent::__construct();
        $this->load->model('Crud_model', 'crud');
        $this->load->helper('url');
    }

    public function index(){
        $this->load->view('crud_view');
    }

     function fetch(){
        $this->load->model('Crud_model');
            $output='';
            $query='';
        if($this->input->post('query')){
                $query = $this->input->post('query');
        }

        $data = $this->Crud_model->fetch_data($query);

        $output .= '
        <table>
                <th>Name</th>
                <th>Description</th>
                <th>Created_at</th>
        ';

        if($data->num_rows() > 0){
                foreach ($data->result() as $row)
                {
                    $output .= '<tr>
                                                <td>'.$row->name.'</td>
                                                <td>'.$row->description.'</td>
                                                <td>'.$row->created_at.'</td>
                                                <td>'.$row->updated_at.'</td>
                                            </tr>';
                }
        }else{
            $output .= '<tr><td> No Data Found </td></tr>';
        }
    }

【问题讨论】:

  • 首先向$.ajax 添加一些错误处理。您还可以在浏览器开发工具网络选项卡中检查实际请求,以查看 url 是否符合预期、请求状态、发送和接收的内容等
  • 您已经包含了 jQuery 2x,这可能会导致错误并且可能会阻止您的 AJAX 运行。所有这些都将在您的浏览器的开发工具中可见,如果您不使用它们,您就是在盲目地工作。

标签: javascript jquery ajax codeigniter-3


【解决方案1】:

我在下面提到的您的代码中发现了一些缺失的内容。

  1. 您尚未创建正确的 HTML 表格。
  2. 您尚未从控制器解析数据,因此您没有收到任何响应。

所以你首先需要在控制器函数的末尾添加json_encode 这样echo json_encode($output);

改变你的功能是这样的:

function fetch(){
    $this->load->model('Crud_model');
        $output='';
        $query='';
    if($this->input->post('query')){
        $query = $this->input->post('query');
    }

    $data = $this->Crud_model->fetch_data($query);

    $output .= '
    <table>
    <thead>
    <th>Name</th>
    <th>Description</th>
    <th>Created_at</th>
    <th>Updated_at</th>
    </thead>
    <tbody>
    ';

    if($data->num_rows() > 0){
        foreach ($data->result() as $row)
        {
            $output .= '<tr>
                <td>'.$row->name.'</td>
                <td>'.$row->description.'</td>
                <td>'.$row->created_at.'</td>
                <td>'.$row->updated_at.'</td>
            </tr>';
        }
    }else{
        $output .= '<tr><td> No Data Found </td></tr>';
    }

    $output .= '</tbody>';
    
    echo json_encode($output);
}

【讨论】:

    【解决方案2】:

    您的代码很好,但您必须显示您的 $output 变量。 不打印变量 ajax 无法从 php 读取数据。 供我们参考:

      $output .= '
        <table>
                <th>Name</th>
                <th>Description</th>
                <th>Created_at</th>
        ';
    
        if($data->num_rows() > 0){
                foreach ($data->result() as $row)
                {
                    $output .= '<tr>
                                                <td>'.$row->name.'</td>
                                                <td>'.$row->description.'</td>
                                                <td>'.$row->created_at.'</td>
                                                <td>'.$row->updated_at.'</td>
                                            </tr>';
                }
        }else{
            $output .= '<tr><td> No Data Found </td></tr>';
        }
    

    回声输出;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-11
      • 2013-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-03
      相关资源
      最近更新 更多