0
Ajax可以成功地提醒發佈數據({"booking":{"testdata":"testdata"}}
)。問題是php代碼無法獲得$_POST
。
兩者的JS和PHP存在於同一頁:localhost/booking/index.ctp
這裏是我的代碼:
<script type="text/javascript" src="//code.jquery.com/jquery-2.1.0.min.js"></script>
<script>
$.ajax({
url: "<?php echo $this->Url->build([
"controller" => "booking",
"action" => "index",
"_ext" => "ctp",
true
]);?>", //This is the current doc
type: "POST",
dataType:'json',
data: {'testdata': 'testdata'} ,
success: function(data){
// console.log(data);
alert(JSON.stringify(data));
}
});
</script>
<?php
if (isset($_POST['testdata'])){
$getv = $_POST['testdata'];
echo json_encode($getv);
}
else {
echo "none";
}
?>
你到底想用這段代碼實現什麼功能? –