关于这个主题有很多,但我不知道为什么它不起作用,它曾经是,现在它停止了。我运行一个查询并使用 php 返回一个对象,然后尝试将其编码为 json,然后将其解析为客户端的 json。它告诉我 SyntaxError:JSON.parse:JSON 数据后出现意外的非空白字符。
我的PHP代码:
$product = ShoppingCart::addToCart($_GET['product_id']);
echo json_encode($product);
客户端代码:
$.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'
);
};
这是返回的内容:
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"}