HoodCoderMan HoodCoderMan - 2 months ago 13x
Javascript Question

JQUERY DATATABLE returning JSON on Document Ready

It's a Code World...

I thought I had completed something similar to this, but apparently not.

I am trying to return JSON from a PHP script to populate the DATATABLE.

Here is the PHP script called 'api/exceptions_all.php':

$select = "SELECT '', [FOR_PARTNER], [FOR_NAME] FROM [brokerage].[dbo].[for_exceptions]";

$query = sqlsrv_query($dbc, $select);

$out = array();
while( $row = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC) )
$out[] = $row;
echo json_encode($out);


When I pull up the file in the browser, I can see the JSON and it is in the correct format:

[{"":"","FOR_PARTNER":"0000111222","FOR_NAME":"CUSTOMER1 "},{"":"","FOR_PARTNER":"0000333111","FOR_NAME":"CUSTOMER2"}]

Here is the JQUERY I was attempting to use to retrieve the JSON:

type: 'GET', // not sure if I should use POST or GET
url: 'api/exceptions_all.php',
dataType: 'json',
async: false,
data: data,
success: function(data)
console.log(data); // I'm guessing the success is where I should populate the table rows
fail: function()

Here is the HTML where I initially set the table:

<table class='table table-bordered display nowrap' id='example1' cellspacing='0' width='100%'>
<th><!--column used for editing--></th>
<th>Partner Code</th>
<th>Partner Name</th>
<tbody id="tableBody">
<!-- this is where the datatable rows should be displayed -->

Further down in the same HTML file, I format the datatable (now that I think about it, I'm not sure if I need this):

<script type="text/javascript">
table = $('#example1').DataTable({
"iDisplayLength": 25,
"autoWidth": true,
"scrollX": true,
"scrollY": 550,
"stateSave": true,
"dataType": "json"

With all of this done, I'm getting a console error that reads:

Uncaught ReferenceError: data is not defined

I've had this before and was never able to recover from it.

Please help me fix this so that I can retrieve the JSON and populate the datatable.

Thank you in advance.


You do not seem to pass data through AJAX to your PHP script 'api/exceptions_all.php'.

So you dont need this line in your jQuery code

data: data

Remove it and the error you get should disappear