我正在尝试从jquery表单验证进行Ajax调用,但它不断返回undefined
。我已经尝试在堆栈溢出中搜索解决方案,但没有任何帮助。这是我的Ajax调用:
jQuery(document).ready(function() {
var form = $("#ClientAdminAddForm");
form.validate({
rules: {
tel: {
number: true,
},
mobile: {
number: true,
},
website: {
url: true
},
client_reference_no:{
remote: {
url: "<?php echo $this->Url->build(array('controller'=>'clients','action'=>'check')) ?>",
type: "post",
cache: "false",
dataType: "json",
data: {
'client_reference_no': function() {
return $( "#client_reference_no" ).val();
},
success: function(response) {
console.log(response);
// console.log(response);
// if (response == '0')
// return false;
// else
// return true;
}
}
},
},
messages: {
client_reference_no: {
remote: "The value is already taken."
}
}
}
});
这是我的控制器动作:
public function check()
{
$this->autoRender = false;
$client_reference_no = isset($this->request->data['client_reference_no']) && !empty($this->request->data['client_reference_no']) ? $this->request->data['client_reference_no']:'';
$pid = isset($this->request->data['pid']) && !empty($this->request->data['pid']) ? $this->request->data['pid']:'';
if (!empty($client_reference_no)) {
if($pid == '') {
if ($this->Clients->exists(['client_reference_no' => $client_reference_no])) {
$status = 0;
}
}
else {
$data = $this->Clients->find()
->where(['client_reference_no' => $client_reference_no])
->select(['id'])
->first()
;
if(!iterator_count($data)) {
$status = 1;
}
else {
if(!empty($data->id) && $data->id == $pid ) {
$status = 1;
}
else {
$status = 0;
}
}
}
}
$this->set([
'output' => $status,
'_serialize'=>'output'
]);
}
我已尝试调试控制器中的$status
并显示该值。但是ajax响应中的值未定义。我也在路由中加载了RequestHandler
组件和集合$routes->setExtensions(['json', 'xml', 'ajax']);
。
console.log()
两次输出undefined
:
我做错了什么?请帮忙!