ajax送出2維元陣列方法及php接收

分享至
■javascript

answer=[{id:0,answer:""},{id:0,answer:""},{id:0,answer:""},{id:0,answer:""},{id:0,answer:""},{id:0,answer:""},{id:0,answer:""},{id:0,answer:""},{id:0,answer:""},{id:0,answer:""}];

$.ajax({
   url:"check.php",
   type:"POST",
   data:{answer_row:answer},
   success: function(data){
       result = JSON.parse(data);//將data轉為json格式
       console.log(result)

       $('#ask').modal('hide');
       $('#result').modal('show');

       resultSizeResize();
   },
   error: function(){
       console.log("檢查題目請求錯誤")
   }
});

■php

<?php

$answer=isset($_POST['answer_row']) ? $_POST['answer_row'] : "";

echo json_encode($answer);

?>

發表迴響