当我将请求类型设置为“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 –