我有一个AJAX后文脚本:
$.ajax({
url: '/products',
data: { product[order_id]:order_id, product[product_id]:product_id, product[count]:count },
type: 'post',
success: function(result){
// $('.resultMessage').text(result);
}
})它给了我一个错误:Uncaught SyntaxError: Unexpected token [ --我如何解决这个问题?
发布于 2015-07-25 16:52:18
您的数据是无效的JSON。试试这个:
$.ajax({
url: '/products',
data: {
product: {
order_id: order_id,
product_id: product_id,
count: count
}
},
type: 'post',
success: function(result){
// $('.resultMessage').text(result);
}
})你会像这样访问它:
var myOrderId = data.product.order_id;发布于 2015-07-25 16:52:56
product_array = // array with key and value pair.
var productString = JSON.stringify(product_array);
$.ajax({
type: "POST",
url: "script.php",
data: {data : productString},
cache: false,
success: function(){
alert("OK");
}
});和php代码如下
$post_data = json_decode(stripslashes($_POST['data']));
// and using foreach you can get key or value as follow.
foreach ($post_data as $key => $value) {
echo $key;
echo $value;
}https://stackoverflow.com/questions/31628756
复制相似问题