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