title: Yii: Render Json Of A Model In Controller Action
body: <p>well I am new to Yii and have a problem: I am sending a ajax POST request (with firstname and lastname parameters) from a Jquery Mobile app to the Yii WorkerController's actionJSON() function. This function (action) retrieves the worker model with column values of submitted firstname and lastname POST values. So here is the code - The problem in the end is that neither of my onSuccess nor onError methods are called in jquery. </p>
  
  <p>JQuery Mobile - </p>
  
  <pre><code>$(document).ready(function() {
          $("#submit").click(function(){
  
              var formData = $("#callAjaxForm").serialize();
  
              $.ajax({
                  type: "POST",
                  url: "localhost/myYiiVodovod1/index.php?r=site/json",
                  //crossDomain: true,
                  cache: false,
                  data: formData,
          //dataType   : 'json',
                  success: onSuccess,
                  error: onError
              });
  
              return false;
          });
      });  
  </code></pre>
  
  <p>Yii workerController - I am not sure whether the findByAttribute() is correct :</p>
  
  <pre><code>public function actionJson(){
  
            if((isset($_POST['firstName']) &amp;&amp; isset($_POST['lastName']))){
  
              header('Content-type: application/json');  
  
              $model=$this-&gt;loadModelTwo($_POST['firstName'],$_POST['lastName']);
  
              echo CJSON::encode($model);  
  
          }
              Yii::app()-&gt;end();  
  
      }
  
  public function loadModelTwo($fname,$lname){
  
          $model = Worker::model()-&gt;findByAttributes(array('first_name' =&gt; $fname, 'last_name' =&gt; $lname));
  
          if($model===null){
          throw new CHttpException(404,'The requested page does not exist.');
          }              
  
          return $model;
  
      }
  </code></pre>
  
  <p>Thanks.</p>