Yii Framework 2.0 check if it is a GET request from AJAX
Working with Yii framework 2.0, I have an AJAX GET jQuery script that points to a function in a controller class.
$.get('localhost/website/index', {param: 'xxxx'}, function(returnedData){
// some code here.....
}, 'json');
In the controller class, I have the following method that handles the AJAX GET request.
public function actionIndex() {
$getParam = $_GET['param'];
// echo $getParam is: 'xxxx'.
// some other code here....
echo json_encode(array());
}
Everything works fine when executing this AJAX GET jQuery script. But if I go to the localhost / website / index link manually in a web browser, I get the following error.
PHP Notice - ErrorException
Undefined index: param
// the code snippet is also being shown.....
I don't want users to see this error if they know this link and accidentally visit this link by accident or on purpose. If i use
if($_GET['param']){...}
I am still getting the error in the browser. How can I solve this?
+3
source to share
2 answers