【问题标题】:How to get ajax response true/false from controller.action如何从 controller.action 获取 ajax 响应真/假
【发布时间】:2012-10-19 11:07:59
【问题描述】:

我想检查客户端名称是否存在然后不创建它。
功能和一切都运行良好。
我需要的是:函数向我的 ajax 函数返回 true 或 false。

我在 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."});
        }
    };

和我在 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);
    }

【问题讨论】:

    标签: javascript php jquery ajax cakephp


    【解决方案1】:

    返回布尔值

    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;                   
    }
    

    检查ajax的成功回调为

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

    【讨论】:

    • 我刚刚做了,但它不起作用:(我什至尝试 $this->set('client_exists',$client_exists); 传递真假,但它不会再加载页面.
    • “不会再加载页面”意味着您的代码中某处存在错误 - 尝试浏览您的 AJAX URL 并查看您收到的错误消息。如果失败,请检查服务器日志
    • @VahidRajaei 检查控制台,是否发生任何请求并检查响应是否有任何错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-02
    • 2021-11-15
    • 2012-07-01
    • 2016-09-24
    • 2018-03-19
    相关资源
    最近更新 更多