【问题标题】:How to get the response and return result from controller action to view如何从控制器操作中获取响应并返回结果以查看
【发布时间】:2014-05-27 05:56:53
【问题描述】:

我已经在 View 中编写了这个脚本。在 onblur 事件中,我检查了邮件 id 是否已经退出 r not.for that I have to pass mailId id to the controller action,我想得到返回结果。

$.ajax({
        type: "POST",
        url: "<?php Yii::app()->createAbsoluteUrl("Approval/checkMailid"); ?>",
        data: mailId,
        success: function() {
          return data;
        },
        error: function() {
            alert('Error occured');
        }
    });

【问题讨论】:

  • 从控制器返回 1 或 0 并使用 if 内的 success 条件来处理该响应

标签: php ajax yii


【解决方案1】:
public function actionCheckMailid($mailId)
{
   $model = YourModel::model()->findAll('id = :mid', array(':mid'=>$mailId));

   echo json_encdoe($model);
}

【讨论】:

    【解决方案2】:

    只需要抛出一个 404 页面,ajax 错误句柄就可以捕捉到它。

    public function actionCheckMailid($mailId){
        $exist = Yii::app()->db->createCommand('select count(*) from your_email_table where id_email = :id_email')
                            ->queryScalar(array(
                                'id_email' => $mailId
                            ));
        if($exist > 0){
            echo json_encode(array(
                'success'=>true
            ));
        }else{
            throw new CHttpException('Email can not be found');         
        }                           
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多