PHP can echo all data, but Ajax only get empty array

PHP can echo all data, but Ajax only get empty array

ghafarizghafariz Posts: 14Questions: 6Answers: 0

So iam using Datatables for 6000 row from database. I'm using SSP CLASS to use the serverside program.
if I'm open the PHP file that echoing the JSON_ENCODE data, it will print out all data from database. But when I'm calling the data with ajax for showing it with Datatables, its only show empty row, and I'm trying to console.log the data, its empty array, what should I do? because datatables is not providing me an error message here

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,237Questions: 1Answers: 2,599

    I'm not too clear what the problem is, I'm afraid. For us to help, please can you link to your page, or at the least post your code here so we can take a look,

    Colin

  • ghafarizghafariz Posts: 14Questions: 6Answers: 0

    im sorry colin, i will give you screenshot about this

    -> datatables
    -> PHP file that accessing the data using ssp class

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395

    Colin said

    please can you link to your page, or at the least post your code here so we can take a look,

    Your screenshot doesn't help.

  • ghafarizghafariz Posts: 14Questions: 6Answers: 0

    i cannot give you access to the page, but the code for table:

    var table2 = $("#tabelrekap").DataTable({
        "processing": true,
        "serverSide": true,
        "ajax": {
            url:"../assets/data/list.php",
            data: {db: 'rekappelatihan'},
            type:"POST"
        },
        "autoWidth": false,
        "lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
        "columnDefs":[
          {
            "className": 'centertext', "targets": [0]
          }]
      });
    

    and the ajax PHP file:

    <?php 
    require "connection.php";
    require 'class/ssp.php'; 
    
    if (isset($_POST['db'])) {
        $table = $_POST['db'];
        $primaryKey = 'id'; 
    
        if ($table == 'rekappelatihan') {
            $columns = array(
                array( 'db' => 'nip',           'dt' => 0 ),
                array( 'db' => 'persid',        'dt' => 1 ), 
                array( 'db' => 'nama',          'dt' => 2 ),
                array( 'db' => 'divisi',        'dt' => 3 )
            );
        }
    
        $dbDetails = array( 
        'host' => $dbhost, 
        'user' => $dbusername, 
        'pass' => $dbpassword, 
        'db'   => $dbname 
        ); 
         
        echo json_encode( 
            SSP::simple( $_POST, $dbDetails, $table, $primaryKey, $columns ) 
        );
    } else header("Location: ../../dashboard");
    
  • colincolin Posts: 15,237Questions: 1Answers: 2,599

    Everything looks OK, try removing the type:"POST",

    Colin

  • ghafarizghafariz Posts: 14Questions: 6Answers: 0

    Hello colin, when i remove the type: "POST", it give me warning Invalid JSON response

  • allanallan Posts: 63,226Questions: 1Answers: 10,416 Site admin
    Answer ✓

    What is the response from the server in that case? The error message should have pointed you to this tech note which explains how you can get that information.

    Also, can you post the type:"POST" back, since that it what you are using for your SSP::simple( $_POST, ...) call.

    Finally, since you can't provide a link, can you use the debugger to give me a trace please - click the Upload button and then let me know what the debug code is.

    Allan

  • ghafarizghafariz Posts: 14Questions: 6Answers: 0

    Sorry guys, turns out the data from database is empty all along, I'm soo sorry for wasting your time guys.. you can delete this post Admin

This discussion has been closed.