I have a big DB data to display. If i do this with just PHP/SQL, it result too big and it take too long for showing data.
So i prefere to display all data with AJAX. But, when i create the PHP page for encode the data to JSON, and then add the header('Content-type: application/json');, i get this error:
SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data at line 1 column 388 of the JSON data
At the moment when i edit the script, and delete/comment the line 'header('Content-type: application/json');', the results are correctly displayed.
So, first, i create a function that take all data that i need:
Orders.php
public function getOrders(){
$db = Database::getInstance();
$fluent = new \Envms\FluentPDO\Query( $db->getConn());
$query = $fluent
->from('en_orders o')
->innerJoin('en_carrier AS c on c.id_carrier = o.id_carrier')
->innerJoin('en_address AS a on a.id_address = o.id_address_delivery')
->innerJoin('en_order_carrier AS oc on oc.id_order = o.id_order')
->select('o.date, o.en_reference, o.id_order, o.id_cart, o.ps_reference, a.firstname, a.lastname, a.address1,
a.address2, a.postcode, a.city, a.telephone, a.mobile, c.name, o.module, oc.colli, oc.weight, o.total_shipping,
o.total_paid_tax_inclu, o.id_order');
return $query ;
}
Then, in view_ordini_ajax.php there is the processing of data. I choose to stock all in a multidimensional array (and i believe that this create the problem):
$ordini = new Orders();
$results = [];
header('Content-type: application/json');
foreach($ordini->getOrders() as $row){
$results = array(
'id_order' => $row['id_order'],
'id_cart' => $row['id_cart'],
'date' => $row['date'],
'en_reference' => $row['en_reference'],
'ps_reference' => $row['ps_reference'],
'firstname' => $row['firstname'],
'lastname' => $row['lastname'],
'address1' => $row['address1'],
'address2' => $row['address2'],
'postcode' => $row['postcode'],
'city' => $row['city'],
'telephone' => $row['telephone'],
'mobile' => $row['mobile'],
'name' => $row['name'],
'module' => $row['module'],
'colli' => $row['colli'],
'weight' => $row['weight'],
'total_shipping' => $row['total_shipping']
);
echo json_encode($results);
};
Normally, the JSON is like that:
{"id_order":TEST,"id_cart":TEST,"date":"2019-06-10 18:24:26","en_reference":"TEST","ps_reference":"TEST","firstname":"TEST","lastname":"TEST","address1":"TEST","address2":"TEST","postcode":"TEST","city":"TEST","telephone":"TEST","mobile":"","name":"Corriere Standard","module":"TEST","colli":1,"weight":"13","total_shipping":"10.000000"}
How can i get the entire json keeping the content-type ? And why i get this error? Thank you!
json_encode
outputs a JSON text and you have it in a loop.So while you are saying you are sending a JSON text, you're actually sending a file with multiple JSON texts all jammed up against each other. The first one parses, then it hits the
{
from the second one after the parser thinks it should be finished … so it errors.for
loop (you are doing this already, but you overwrite it)$results
into that array ($results[] = array(etc
— note the[]
!).echo json_encode($results);
after the loop instead of `inside the loop.You also need to make sure your client is expecting to receive an array of data from the JSON.