Javascript 如何从controller.action获取ajax响应true/false
我想检查客户端名称是否存在,然后不创建它。Javascript 如何从controller.action获取ajax响应true/false,javascript,php,jquery,ajax,cakephp,Javascript,Php,Jquery,Ajax,Cakephp,我想检查客户端名称是否存在,然后不创建它。 功能和每件事都很完美。 我需要的是:函数向我的ajax函数返回true或false。 我在js和ajax中的创建功能是: function createClient(){ var newClientForm = $$('newClientForm').getValues(); var client_name = newClientForm.client_name; if (client_name != '')
功能和每件事都很完美。
我需要的是:函数向我的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);
}
返回布尔值
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,查看收到的错误消息。如果失败,请检查服务器logs@VahidRajaei检查控制台,是否有任何请求发生,并检查响应是否有错误。