0

i am trying to auto complete department using jquery autocomplete library. ajax call is as follows

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

my controller has a action called getDepartment is as follows:

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

here i need to send the $departments as Json .

  1. how to send the response as JSON
  2. why controller is not reaching autocomplete success function (where i have put an alert)

when i run i get response (using fireBug) as

      [{"Department":{"name":"Testing","id":"1"}},{"Department":{"name":"Testing","id":"3"}},{"Department":{"name":"Testing2","id":"6"}},{"Department":{"name":"testing","id":"7"}},{"Department":{"name":"test","id":"8"}}]  

1 Answer 1

1

Your response is valid JSON and so your dataType must be too.

dataType: "json"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.