Reputation: 7003
I have this AJAX
post script:
$.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);
}
})
And it gives me this error: Uncaught SyntaxError: Unexpected token [
- how could I fix this?
Upvotes: 0
Views: 64
Reputation: 339
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");
}
});
and php code as follow
$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;
}
Upvotes: 0
Reputation: 13620
Your data is not valid JSON. Try this:
$.ajax({
url: '/products',
data: {
product: {
order_id: order_id,
product_id: product_id,
count: count
}
},
type: 'post',
success: function(result){
// $('.resultMessage').text(result);
}
})
And you'd access it like this:
var myOrderId = data.product.order_id;
Upvotes: 1