How can write this code safer?

I have an Ajax.

How can write this code safer (in controller and view)?

Should I use try-catch? If yes How do it?


$.ajax({

	'url': '".$this->createUrl('index')."',

	'type': 'post',

	'data': {'Id': id,'t': t},

	'dataType': 'json',

	'success': function(data){},

		});

controller:


if (Yii::app()->request->isAjaxRequest) {

	if(isset($_POST['id']) && isset($_POST['t'])){

		$id = intval($_POST['id']);

		$t = intval($_POST['t']);

		$ss = Model::model()->findByPk($id);

		$result = $ss->data;	

		echo CJSON::encode(array(

		  'result' => $result,

		));

		Yii::app()->end();

			}

		}

		$this->render('index',array(

			'model'=>$model,

		));

for ajax part, instedd of try/catch

$.ajax({

    'url': '".$this->createUrl('index')."',


    'type': 'post',


    'data': {'Id': id,'t': t},


    'dataType': 'json',


  }).done: (function(data){


     // means response code is 200


  }).fail(function(data) {


       // here data is server response, and also means that code != 200


       if (data.responseText) // for custom exceptions


         alert(data.responseText);


       else 


         alert (data.statusText); //something like 'Bad Request'


  })
1 Like