I have an AJAX call in my view to an action in my controller. However, I always get an Error 400. Below is the code for my AJAX call:
$.ajax({
cache: false,
url: '/dummy/index.php/module/controller/checkCross',
dataType: 'json',
type: 'POST',
data : {"male":parents[0],"female":parents[1]},
success: function(result){
alert(result);
}
});
Below is the code in the controller:
public function actionCheckCross(){
if(Yii::app()->request->isPostRequest) { // check if POST
$flag = CrossingMatrix::model()->checkParentsIfCrossed($_POST['male'],$_POST['female']);
if($flag){
return true;
}
else{
return false;
}
} else { // direct URL request will be GET, so show error
throw new CHttpException(400, Yii::t('app', 'Direct access is not allowed.'));
}
}
Any ideas?
You are expecting json data, but you send empty page to browser. You should encode result like that:
echo CJavascript::jsonEncode((bool)$flag);
Yii::app()->end();
In your code you returned value. Notice that your yii exception message is Direct access is not allowed but your error is Your request is invalid.