There is alot on this subject but I can't figure out why it's not working, it was, now it stopped. I run a query and return an object with php, then try to encode it to json, then parse it client side to json. It is telling me that SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data.
My php code:
$product = ShoppingCart::addToCart($_GET['product_id']);
echo json_encode($product);
client side code:
$.get(
'./models/shoppingCart.inc.php',
{product_id: id},
function(data) {
var product = JSON.parse(data)
$('#cart_qty').html(cartQty);
var table=document.getElementById("table_products");
var row=table.insertRow(1);
var cell1=row.insertCell(0);
var cell2=row.insertCell(1);
var cell3=row.insertCell(2);
cell1.innerHTML=product.product_name;
cell2.innerHTML=product.price;
cell3.innerHTML=qty;
},
'html'
);
};
this is what is getting returned:
16{"product_id":"16","product_name":"Pavlova","price":"17.4500","supplier":{"company_name":"Pavlova, Ltd.","address":"74 Rose St.\r\nMoonie Ponds","phone":"(03) 444-2343","city":null,"contact_name":"Ian Devling","contact_title":"Marketing Manager"},"units_in_stock":"29","units_on_order":"0","reorder_level":"10","category_id":"3","qty_per_unit":"32 - 500 g boxes","discontinued":"0","supplier_id":"7"}
Barmar
789k57 gold badges555 silver badges669 bronze badges
1 Answer 1
You have some debug output in your php script or there is content before the opening <?php tag. The 16 in front of the json has to be removed. Find out where it comes from and remove it.
answered Nov 4, 2013 at 18:23
hek2mgl
159k31 gold badges263 silver badges280 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
default
16before the PHP code you showed.echo $_GET['product_id'];before you call theecho json_encode($product);somewhere?