I got an array from an SQL query, encoded it and when I decode in javascript I receive the error
Unexpected token a in JSON at position 0
The var_dump of the array before the encode:
array(1) { [0]=> array(10) { ["order_id"]=> string(10) "1512642144"
["order_erp_id"]=> string(1) "0" ["order_type"]=> string(3) "web"
["order_date"]=> string(23) "2017-12-07 11:22:24.263" ["order_total"]=>
string(4) "0.00" ["order_desc1"]=> string(3) "web" ["order_desc2"]=>
string(4) "test" ["no_items"]=> string(1) "0" ["order_count"]=> int(1)
["item_arr"]=> string(0) "" } }
[{"order_id":"1512642144","order_erp_id":"0","order_type":"web",
"order_date":"2017-12-07 11:22:24.263","order_total":"0.00",
"order_desc1":"web","order_desc2":"test","no_items":"0",
"order_count":1,"item_arr":""}]
then I encode and send it with:
echo json_encode($order_item_arr);
On the client side, I receive the data and before the decoding step I have this from the console.log(data); in javascript:
array(1) {
[0]=>
array(10) {
["order_id"]=>
string(10) "1512642144"
["order_erp_id"]=>
string(1) "0"
["order_type"]=>
string(3) "web"
["order_date"]=>
string(23) "2017-12-07 11:22:24.263"
["order_total"]=>
string(4) "0.00"
["order_desc1"]=>
string(3) "web"
["order_desc2"]=>
string(4) "test"
["no_items"]=>
string(1) "0"
["order_count"]=>
int(1)
["item_arr"]=>
string(0) ""
}
}
[{"order_id":"1512642144","order_erp_id":"0","order_type":"web",
"order_date":"2017-12-07 11:22:24.263","order_total":"0.00",
"order_desc1":"web","order_desc2":"test","no_items":"0",
"order_count":1,"item_arr":""}]
So it looks like quite ok, and when I decode it with JSON.parse(data); I have the error:
VM414:1 Uncaught SyntaxError: Unexpected token a in JSON at position 0