我使用Ajax调用向php脚本发送数组,但我无法在php中访问该数组。
这是我的代码:
seats = ["s4","s6","s9","s24"];
sendBookedSeats(seats);
function sendBookedSeats(seats){
console.log(seats);
$.ajax({
type: "POST",
url: "index.php",
data: {
'seats' : seats,
}
})
}
如何在我的php脚本中访问数组seats
?
答案 0 :(得分:1)
以json发送数据:
seats= ["s4","s6","s9","s24"];
sendBookedSeats(seats);
function sendBookedSeats(seats){
console.log(seats);
$.ajax({
type: "POST",
url: "index.php",
data: {
'seats' : JSON.stringify(seats),
}
})
}
PHP:
json_decode($_POST["seats"]);
答案 1 :(得分:0)
function sendBookedSeats(seats){
$.ajax({
type: "POST",
contentType: 'application/json',
url: "index.php",
data: {
'seats' : seats,
}
})
}
将内容类型添加到您的ajax调用中。