【问题标题】:Ajax Autocomplete in cakePhp- NetworkError: 404 Not FoundcakePhp 中的 Ajax 自动完成- NetworkError: 404 Not Found
【发布时间】:2012-04-09 11:55:34
【问题描述】:

我正在尝试通过使用我的视图页面中的 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 调用时,我都会遇到错误

“NetworkError: 404 Not Found - http://localhost/employees/getAddress?callback=jQuery1710012251482368207167_1333972423088&featureClass=P&style=full&maxRows=12&name_startsWith=sal&_=1333972426249

如何解决这个问题?

【问题讨论】:

    标签: javascript ajax cakephp jquery


    【解决方案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 并使用它

    【讨论】:

    • C:\xampplite\htdocs\cakephp\app 这是我的应用程序的路径
    • 你能告诉我如何给出正确的网址吗? url:“app/employees/getAddress”有意义吗?
    • 我正在尝试以同样的方式自动完成,但是当我从 ajax 调用接收数据时,我不确定如何调用响应函数。您能否扩展答案以显示如何调用该响应回调?
    猜你喜欢
    • 2015-03-20
    • 2015-02-12
    • 2014-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-16
    • 2019-04-06
    相关资源
    最近更新 更多