當我將請求類型設置爲「GET」(並且在服務器端也使用$ _GET)時,它成功獲取響應 ,但給出一個400 error
,Missing required parameters: student_id
當我將類型設置爲POST。爲什麼在請求方法爲GET時此ajax請求成功但在POST方法中失敗
下面的代碼:
$.ajax({
type: "GET",
url: "?r=fees/fees/transactions",
dataType: "json",
data: { student_id: student_id },
success:function(msg) {
console.log(msg);
},
error: function(xhr, ajaxOptions, thrownError){
console.log("failed");
console.log(xhr.responseText);
console.log(ajaxOptions);
console.log(thrownError);
}
});
這是當我設置請求方法爲GET請求URL:
http://localhost/demo.git/index.php?r=fees/fees/transactions&_csrf=bEJJWVowdl8jBwQjaUMsAA52eT8MXBMLJigwHTxeKSlVKyxoD2o5KQ%3D%3D&student_id=10115"
。
爲什麼當我將上面的請求類型設置爲POST並通過POST方法接收服務器端的變量時,這不起作用?
這裏是服務器端操作(我用Yii2 MVC框架)
public function actionTransactions($student_id){
$student_id = $_POST['student_id'];
...
...
echo json_encode($response);
}
downvoter,關心評論? –
如何在$ _POST中獲得$ _GET方法參數? –
如果你不知道方法是$ _GET還是$ _POST –