Why am I STILL getting a Invalid JSON error?

Why am I STILL getting a Invalid JSON error?

JordanPHPJordanPHP Posts: 6Questions: 3Answers: 0

I am trying to load data into the table with ajax and it just will not load, and I keep getting the invalid JSON response error, although when I pull the returned data out of the developer tool response, it's valid.

This is my PHP/SQL that is pulling the data:

<?php 
header('Content-Type: application/json');
$output = array('data' => array());
$query = "SELECT * FROM table"; 
$stmt = sqlsrv_query($sapconn2, $query);

$x = 1;

while($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)){

    $output['data'][] = array(
            //'col_1' => $x,
            'ID' => $row['ID'],
            'QuoteID' => $row['QuoteID'],
            'CardCode' => $row['CardCode'],
            'SlpCode' => $row['SlpCode'],
            'SlpName' => $row['SlpName'],
            'BandA' => $row['BandA'],
            'NewPrice' => $row['NewPrice']
        );
    $x ++;
}

echo json_encode($output);
?>

This is my ajax request:

var testTable= $("#testTable").DataTable({
    ajax: "test.php",
    columns: [
        { "data": "ID" },
        { "data": "QuoteID" },
        { "data": "CardCode" },
        { "data": "SlpCode" },
        { "data": "SlpName" },
        { "data": "BandA" },
        { "data": "NewPrice" }
    ]
});

This is some example data that is being returned:

{
"data": [{
    "ID": 138,
    "QuoteID": 25,
    "CardCode": "000123",
    "SlpCode": "222",
    "SlpName": "test data",
    "BandA": 222,
    "NewPrice": 222
}, {
    "ID": 144,
    "QuoteID": 25,
    "CardCode": "000123",
    "SlpCode": "132",
    "SlpName": "test data",
    "BandA": 465,
    "NewPrice": 789
}
}

Has anyone had issues like this before, or know how to get around this?

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,871Questions: 1Answers: 10,522 Site admin

    The JSON shown above is invalid - there is a missing closing ].

    Aside from that, if you have a link to the page, or use the debugger on the page I can take a closer look.

    Allan

  • JordanPHPJordanPHP Posts: 6Questions: 3Answers: 0
    edited March 2017

    Thanks for the reply! It does have the closing ']' i just missed it out when copying the data to the question.

    I have used the debugger you suggested it gave me this link:

    http://debug.datatables.net/iyulix

  • JordanPHPJordanPHP Posts: 6Questions: 3Answers: 0
    edited March 2017

    The link was changed as I deleted the data by accident.

  • allanallan Posts: 63,871Questions: 1Answers: 10,522 Site admin
    Answer ✓

    In the debugger link you give, if you click the "Tables" button and then scroll down to the "Server interaction" title and click it, it will show you want is being returned from the server.

    Initially it looks like good JSON. But then scroll down and you'll see that it includes HTML as well, which is why it is invalid JSON.

    Without being able to inspect your server-side code, I don't know what would be causing that. It looks like a page include.

    Allan

  • JordanPHPJordanPHP Posts: 6Questions: 3Answers: 0

    OMG Thank you!!! it was so simple, I was including my normal page template as an include! I deleted that and it worked straight away! Thanks again.

This discussion has been closed.