Home > Blockchain >  Entire php file returns to datatable ajax call
Entire php file returns to datatable ajax call

Time:05-02

I tried to get a php response for server side processing of data. I expected JSON file returned but the entire php file returned.

I treid both Xampp and WAMP but the problem didn't solved. Both Xampp and Wamp can excute the php file on browser at http://localhost:8080//example.php without problem.

My javscript and php code as below

<script>
    $(document).ready(function() {
        $('#example').DataTable( {
            "processing": true,
            "serverSide": true,
            "ajax": {url : "example.php",
                     type: "GET",
                     dataType: "JSON"
                    },
            "columns": [
    { "data": 'chr'},
    { "data": 'pos'}
    ]
        } );
    } );
</script>

    <?php
 
// DB table to use
$table = 'homo_sapiens';
 
// Table's primary key
$primaryKey = 'dRid';
 
// Array of database columns which should be read and sent back to DataTables.
// The `db` parameter represents the column name in the database, while the `dt`
// parameter represents the DataTables column identifier. In this case simple
// indexes
$columns = array(
    array( 'db' => 'chr', 'dt' => 'chr' ),
    array( 'db' => 'pos',  'dt' => 'pos'),

);
 

// SQL server connection information
$sql_details = array(
    'host' => 'localhost',
    'user' => 'root',
    'pass' => '',
    'db'   => 'XXXXX'
);
 
 
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * If you just want to use the basic configuration for DataTables with PHP
 * server-side, there is no need to edit below this line.
 */
 
require( 'ssp.class.php' );
 
echo json_encode(
    SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns)
);


?>

The response on http://localhost:8080/example.php:

{"draw":0,"recordsTotal":114,"recordsFiltered":114,"data":[{"chr":"chr1","pos":"631704"},{"chr":"chr1","pos":"631714"},{"chr":"chr1","pos":"631848"},{"chr":"chr1","pos":"632344"},{"chr":"chr1","pos":"632461"},{"chr":"chr1","pos":"633143"},{"chr":"chr1","pos":"633300"},{"chr":"chr1","pos":"633364"},{"chr":"chr1","pos":"633839"},{"chr":"chr1","pos":"633852"},{"chr":"chr1","pos":"633860"},{"chr":"chr1","pos":"634183"},{"chr":"chr1","pos":"634249"},{"chr":"chr1","pos":"1014207"},{"chr":"chr1","pos":"1014246"},{"chr":"chr1","pos":"1055942"},{"chr":"chr1","pos":"1311386"},{"chr":"chr1","pos":"1311563"},{"chr":"chr1","pos":"1311637"}, ..................

A screen shot for Network reponse:

enter image description here

I have been stucked with it for several days. Any help are highly appreciated!

CodePudding user response:

Make sure that the call from your Javascript is accessing it via http and does include the host, otherwise Apache might not recognize it. Like this: http://localhost:8080/example.php. Otherwise it may read it as a file and return what you're currently getting since it's all running locally. I'm not absolutely certain how Datatables grabs the info but it may be setup that way to use raw data like .json or .xml files, or in your case, your .php file. So without the http://localhost it may be automatically treating it like just a file read.

Another possibility is your apache config may be missing the PHP mimetype. You could add it in the httpd.conf like this:

AddType application/x-httpd-php .php

Although I find that unlikely as you're able to execute it normally.

Report back, let me know if that fixes it!

  • Related