0

我正在尝试通过在我的视图页面中使用 jquery UI 库(jquery 自动完成)来进行 ajax 调用,以根据用户输入获取部门名称。自动完成功能看起来像

$( "#auto_complete" ).autocomplete({
        source: function( request, response ) {
            $.ajax({
                url:  "/employees/getAddress",
                dataType: "jsonp",
                data: {
                    featureClass: "P",
                    style: "full",
                    maxRows: 12,
                    name_startsWith: request.term
                },
                success: function( data ) {

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

在这里,我正在呼吁采取行动,称为 getAddress()

看起来像

       class EmployeesController extends AppController {

//var $components = array('Autocomplete');
public $components = array('RequestHandler');
var $helpers = array('Javascript', 'Ajax');
    /**
     * index method
     *
     * @return void
     */
    //var $helpers = array('Ajax');
 public function getAddress() {
        $this->log($this->params, 'debug');
        $this->layout = 'ajax';
        $departments = $this->Department->find('all', array(
        'conditions'=>array('Department.name LIKE'=>$this->params['url']['q'].'%'),
        'fields'=>array('name', 'id')));
        $this->log($departments, 'debug');
        //$this->set('departments', $departments);
        $this->set(compact('departments'));
}

但是每当我进行ajax调用时,我都会出错

“网络错误:404 未找到 - http://localhost/employees/getAddress?callback=jQuery1710012251482368207167_1333972423088&featureClass=P&style=full&maxRows=12&name_startsWith=sal&_=1333972426249

如何解决这个问题呢?

4

1 回答 1

1

我猜您在 url 中缺少项目文件夹,请尝试:


var webroot = '<?php echo $this->webroot; ?>';
$( "#auto_complete" ).autocomplete({
        source: function( request, response ) {
            $.ajax({
                url:  webroot + "employees/getAddress",
                dataType: "jsonp",
                data: {
                ......

或者您可以在 layouts/default.ctp 中定义 webroot 并使用它

于 2012-04-09T12:06:10.190 回答