You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CodeIgniter中Ajax请求无法正常工作问题求助

Troubleshooting Your CodeIgniter Ajax Data Retrieval Issue

Hey there, sorry to hear you’ve been stuck on this for 6 hours—let’s walk through some common fixes to get your Ajax call returning data from the database.

First, let’s recap what we know: you’re self-learning CodeIgniter, your Ajax call isn’t returning any results, Chrome DevTools shows no errors, and you’ve shared parts of your Model and Controller constructor. Let’s break down the possible issues step by step.

1. Verify Your Controller Has a Complete Ajax Handler Method

The constructor code you shared looks fine, but you’re missing the actual controller method that handles the Ajax request. Without this, your Model’s member_id method never gets called properly. Here’s what that method should look like:

public function fetch_member_data() {
    // Get POST data from the Ajax request
    $postData = $this->input->post();
    
    // Load your model (if not already loaded in constructor)
    $this->load->model('Your_Model_Name'); // Replace with your actual model name
    
    // Call the model method and get results
    $response = $this->Your_Model_Name->member_id($postData);
    
    // Return the result as JSON (critical for Ajax to parse)
    echo json_encode($response);
    exit; // Exit after echoing to avoid extra output breaking the JSON
}
  • Double-check that this method is public so it’s accessible via URL.
  • Confirm your Ajax request’s url points to this method correctly (e.g., <?php echo base_url('Your_Controller/fetch_member_data'); ?>).

2. Debug Your Ajax Request

Even if DevTools shows no errors, let’s confirm the request is sending data correctly:

  • Open Chrome’s Network tab, trigger the Ajax call, and click on the request entry.
    • Check the Request Method is POST.
    • Go to the Payload tab to verify member_id is being sent with the correct value.
    • Check the Response tab—if it’s empty, that means either no data was returned from the Model, or the Controller isn’t echoing anything.

3. Add Debugging to Your Model

Let’s add some debug checks to your Model to see where things might be failing:

public function member_id($postData) { 
    $response = array(); 
    
    // Debug: Check if member_id is being received
    if(empty($postData['member_id'])) {
        var_dump('No member_id received in model');
        return $response;
    }
    
    var_dump('Received member_id: ' . $postData['member_id']); // Print the received ID
    
    $this->db->select('*'); 
    $this->db->where('member_id', $postData['member_id']); 
    $q = $this->db->get('tbl_test'); 
    
    // Debug: Print the actual SQL query to verify it's correct
    echo $this->db->last_query(); 
    
    $response = $q->result_array(); 
    
    // Debug: Print the query result
    var_dump($response); 
    
    return $response; 
}

Now, test this by sending a POST request directly to your Controller’s fetch_member_data method (use Postman or curl if you prefer). This will show you:

  • If the member_id is being passed correctly.
  • If the SQL query is formatted as expected (check for typos in table/field names).
  • If the query actually returns any data from your tbl_test table.

4. Check for CSRF Protection Issues

If CodeIgniter’s CSRF protection is enabled (it’s on by default), your Ajax request needs to include the CSRF token. Add this to your Ajax setup:

// In your view, first define the CSRF variables
<script>
var csrfToken = "<?php echo $this->security->get_csrf_hash(); ?>";
var csrfName = "<?php echo $this->security->get_csrf_token_name(); ?>";
</script>

// Then in your Ajax call:
$.ajax({
    url: "<?php echo base_url('Your_Controller/fetch_member_data'); ?>",
    type: "POST",
    data: {
        member_id: yourMemberIdValue,
        [csrfName]: csrfToken // Include the CSRF token here
    },
    success: function(data) {
        console.log(data);
    }
});

Without the token, CodeIgniter might silently reject the request, leading to an empty response.

5. Confirm Database and Table Details

Double-check these basics that are easy to overlook:

  • Is your tbl_test table actually present in the database?
  • Does the member_id field exist in the table, and does it match the data type you’re sending (e.g., if it’s an integer, make sure you’re sending a number, not a string)?
  • Are there any rows in tbl_test that match the member_id you’re sending? Sometimes we assume data exists, but it’s worth verifying directly via phpMyAdmin or a SQL command line.

Start with these steps—chances are one of them will uncover why your Ajax call isn’t returning data. Let me know if you find anything specific!

内容的提问来源于stack exchange,提问作者Kiran RS

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 03:14:50