如何使用Ajax从MSSQL Query for DataTables获取数据

This is the code that i get from DataTables Child Rows

$(document).ready(function() {
    var table = $('#example').DataTable( {
        "ajax": "../ajax/data/objects.txt", //here
        "columns": [
            {
                "className":      'details-control',
                "orderable":      false,
                "data":           null,
                "defaultContent": ''
            }, //and here to fetch the data below
            { "data": "name" }, 
            { "data": "position" }, 
            { "data": "office" },
            { "data": "salary" }
        ],
        "order": [[1, 'asc']]
    } );

I would like to get the data from an SQL Query by using ajax. This is my SQL Query :

$tsql = 
"SELECT *
FROM [dbo].[ITEM_MASTER] A
INNER JOIN
[dbo].[STOCK] B
ON
B.ItemId = A.ItemId
";
$result = sqlsrv_query($conn, $tsql, array(), array( "Scrollable" => SQLSRV_CURSOR_KEYSET ));
if (!$result) {
 die("Query to show fields from table failed");
}

while($row=sqlsrv_fetch_array($result))
{
    $ItmId = $row['ItemId'];
    $ItmName = $row['ItemName'];
    $ItmType = $row['ItemType'];
    $ItmGroup = $row['ItemGroup'];
    $ItmClass = $row['ItemClass'];
    $ItmSerialNum = $row['ItemSerialNum'];
    $ItmUOM = $row['ItemUOM'];
    $StkQty = $row['StockQuantity'];
    $StkId = $row['StockId'];
 }

And on the ajax part, i just call the name of the variable like $ItmId or like i've stated in my while loop. Is it possible? If so, how? Since i don't have any knowledge on AJAX


UPDATE

The data were pushed to the second parameter and is not showing the ItmId ? And no matter if i change $data : ItmName it just displays based on the arrays and display something else?

$(document).ready(function() {
    var table = $('#table').DataTable( {
        "ajax": {
            "url": "table_data.php",
            "type": "POST"
            },
        "columns": [
            {
                "class":          'details-control',
                "orderable":      false,
                "data":           null,
                "defaultContent": ''
            },
            { "$data": "ItmId"  },
            { "$data": "ItmName" },
            { "$data": "ItmGroup"},
            { "$data": "ItmClass"}
    ],
        "order": [[1, 'asc']]
    } );

Firstly, you need to change this line:

"ajax": "../ajax/data/objects.txt", //here

to point to the actual file that will run the sql query to fetch the data: i.e. like this:

"ajax": {"url": "path/to/phpfile.php", "type": "POST"}

You need to make your while loop like this:

$data = array();
while($row=sqlsrv_fetch_array($result))
{
    $ItmId = $row['ItemId'];
    $ItmName = $row['ItemName'];
    $ItmType = $row['ItemType'];
    $ItmGroup = $row['ItemGroup'];
    $ItmClass = $row['ItemClass'];
    $ItmSerialNum = $row['ItemSerialNum'];
    $ItmUOM = $row['ItemUOM'];
    $StkQty = $row['StockQuantity'];
    $StkId = $row['StockId'];

    $data['data'][] = array($ItmId, $ItmName, $ItmType,....etc);
 }
 echo json_encode($data);

You should note, you require the exactly amount of columns in your actual table (html). Also, your json should look like this:

data:
    array(
        ItmId,
        ItmName,
        ..etc
    ),
    array(
        ItmId,
        ItmName,
        ..etc
    ),

Essentially have an array of rows.

Based on Darren's answer, i solved it like this:

table_data.php

    $data = array();

    while($row=sqlsrv_fetch_array($result))
    {
    $data['data'][] = array(
                        'ItmId'          => $row['ItemId'],
                        'ItmName'        => $row['ItemName'],
                        'ItmType'        => $row['ItemType'],
                        'ItmGroup'       => $row['ItemGroup'],
                        'ItmClass'       => $row['ItemClass'],
                        'ItmSerialNum'   => $row['ItemSerialNum'],
                        'ItmUOM'         => $row['ItemUOM'],
                        'StkQty'         => $row['StockQuantity'],
                        'StkId'          => $row['StockId']
                        );

    }

   echo json_encode($data);

table.php (where i display my html)

$(document).ready(function() {
    var table = $('#table').DataTable( {
        "ajax": {
            "url": "table_data.php",
            "type": "POST"
            },
        "columns": [
            {
                "class":          'details-control',
                "orderable":      false,
                "data":           null,
                "defaultContent": ''
            },
            { "data": "ItmId"},
            { "data": "ItmName"},
            { "data": "ItmClass"},
            { "data": "ItmUOM"}
    ],
        "order": [[1, 'asc']]
    } );