Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何从控制器操作获取响应并返回结果以查看_Php_Ajax_Yii - Fatal编程技术网

Php 如何从控制器操作获取响应并返回结果以查看

Php 如何从控制器操作获取响应并返回结果以查看,php,ajax,yii,Php,Ajax,Yii,我已经在视图中编写了此脚本。在onblur事件中,我必须检查邮件id是否已经存在。为此,我必须将邮件id传递给控制器操作,并希望获得返回结果 $.ajax({ type: "POST", url: "<?php Yii::app()->createAbsoluteUrl("Approval/checkMailid"); ?>", data: mailId, success: function() {

我已经在视图中编写了此脚本。在onblur事件中,我必须检查邮件id是否已经存在。为此,我必须将邮件id传递给控制器操作,并希望获得返回结果

$.ajax({
        type: "POST",
        url: "<?php Yii::app()->createAbsoluteUrl("Approval/checkMailid"); ?>",
        data: mailId,
        success: function() {
          return data;
        },
        error: function() {
            alert('Error occured');
        }
    });
$.ajax({
类型:“POST”,
url:“”,
数据:mailId,
成功:函数(){
返回数据;
},
错误:函数(){
警报(“发生错误”);
}
});

只需抛出一个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');         
    }                           
}

从控制器返回1或0,并使用
if
中的
success
条件来处理该响应
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');         
    }                           
}