简体   繁体   English

如何从controller.action获取ajax响应是/否

[英]How to get ajax response true/false from controller.action

I want to check if the client name exists then Not to create it. 我想检查客户端名称是否存在,然后不创建它。
the functions and every thing is working perfectly. 功能和所有功能均正常运行。
What I need is: Function return true or false to my ajax function. 我需要的是:函数对我的ajax函数返回true或false。

my create function in js and ajax is: 我在js和ajax中的创建函数是:

function createClient(){
       var newClientForm = $$('newClientForm').getValues();
       var client_name = newClientForm.client_name;

       if (client_name != ''){
       url_call = window.server_name + "/index.php/clients/create_new_client/"+client_name; 
       url_call = url_call.replace(/\n/g, '|');
       $.ajax({
              url: url_call ,               
              success: function(data) {
                       if ((data==true)||(data==false)){
                            alert('data true');
                        }


              }     
        }); 
        }else{            
            dhx.alert({title:'Error', message:"Please enter the client name."});
        }
    };

and my controller in cakephp 和我在cakephp中的控制器

function create_new_client($string = null){
            $this->layout = 'default_really_empty';
            $string = urldecode($string);
            $explode_item = explode('^', $string);

            $client_name        = $explode_item[0]; 

            //--Sreach if the Client is already there don't create it
            $modelClassChild = 'Client';
            $this->loadModel($modelClassChild);                
            $objects=$this->$modelClassChild->find('all',array('conditions'=>array($modelClassChild .'.deleted'=>'0', 
                                                                                   $modelClassChild .'.name'=>$client_name)));
            foreach($objects as $object) { 
                $dbName = $object['Client']['name'];
            }                

            if ($client_name != $dbName){  
            //--Sreach if the Client is already there don't create it
                $clientArray = array();
                $fields = array();

                $clientArray['Client']['name']  = $client_name;
                $fields[] = 'name';

                $client_exists = FALSE;                    

                $this->Client->create();
                $this->Client->save($clientArray, true); 
           }else{
               $client_exists = TRUE;                   
           }
           $this->set('client_exists',$client_exists);
    }

Return the boolean value 返回布尔值

if ($client_name != $dbName){  
    //--Sreach if the Client is already there don't create it
    $clientArray = array();
    $fields = array();

    $clientArray['Client']['name']  = $client_name;
    $fields[] = 'name';


    $this->Client->create();
    $this->Client->save($clientArray, true); 

    return false;                    

}else{
    return true;                   
}

Check the sucess callback of ajax as 检查ajax的成功回调为

success: function(data) {
    if ((data==0)||(data==1)){
        alert('data true');
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM