0

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"}
2
  • 3
    Some other part of the script is echoing 16 before the PHP code you showed. Commented Nov 4, 2013 at 18:23
  • Are you doing echo $_GET['product_id']; before you call the echo json_encode($product); somewhere? Commented Nov 4, 2013 at 18:26

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.