CakePhp将响应作为Json发送并显示
我正在尝试使用jquery自动完成库自动完成部门。 ajax调用如下CakePhp将响应作为Json发送并显示,cakephp,jquery,cakephp-2.1,Cakephp,Jquery,Cakephp 2.1,我正在尝试使用jquery自动完成库自动完成部门。 ajax调用如下 $( "#auto_complete" ).autocomplete({ source: function( request, response ) { $.ajax({ url: "/employees/getDepartment" dataType: "jsonp", //da
$( "#auto_complete" ).autocomplete({
source: function( request, response ) {
$.ajax({
url: "/employees/getDepartment"
dataType: "jsonp",
//dataType: "text/html",
data: {
featureClass: "P",
style: "full",
maxRows: 12,
name_startsWith: request.term
},
success: function( data ) {
alert("success--");
response( $.map( data.geonames, function( item ) {
return {
label: item.name,
value: item.id
}
}));
}
});
},
minLength: 2,
select: function( event, ui ) {
log( ui.item ?
"Selected: " + ui.item.label :
"Nothing selected, input was " + this.value);
},
open: function() {
$( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
},
close: function() {
$( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
}
});
我的控制器有一个名为getDepartment的操作,如下所示:
public function getDepartment() {
$this->log($this->params->query['name_startsWith'] , 'debug');
$str = $this->params->query['name_startsWith'];
$this->log($str, 'debug');
$name='Departmet';
$this->layout = 'ajax';
$departments = $this->Employee->Department->find('all', array( 'recursive' => -1,
'conditions'=>array('Department.name LIKE'=>$str.'%'),
'fields'=>array('name', 'id')));
$this->set('departments',$departments);
}
这里我需要将$departments作为Json发送
您的响应是有效的JSON,因此您的
数据类型必须也是有效的
dataType: "json"
dataType: "json"