0

I'm trying to move some AJAX code from a standalone file into a function in my controller and can't seem to get it to display the JSON data in the autocomplete function in the view. I've verified that the function does return JSON encoded data by visiting the function URL directly.

Here's my JavaScript from the head of my view:

<script type="text/javascript">
$(document).ready(function(){
var ac_config = {
    source: <?php echo base_url() . 'admin/lookup_tmdb_movie_titles'; ?>
    select: function(event, ui){
        $("#title").val(ui.item.title);
        $("#year").val(ui.item.year);
        $("#imdb_link").val(ui.item.imdb_link);
    },
    minLength:2,
    position: {
        my: "left top",
        at: "left bottom",
        collision: "none",
        of: "#title.ui-autocomplete-input.ui-autocomplete-loading"
    }
};
$("#title").autocomplete(ac_config);
});
</script>

Here's the function in the admin controller (I'm just using hard-coded test data until I get this working correctly):

function lookup_tmdb_movie_titles()
{
    $term = 'test';

    // TEST DATA
    $title['title'] = 'test';
    $title['label'] = 'test (2012)';
    $title['value'] = 'test';
    $title['year'] = '2012';
    $title['imdb_link'] = 'testlink';
    $matches[] = $title;

    // convert into JSON format and output
    $matches = array_slice($matches, 0, 5);

    $this->output->set_output( json_encode($matches) );
}

I've also tried outputting the JSON the following two ways, all of which work if I go to the function directly via the URL, but none of which work in the view itself.

    print json_encode($matches);

and

    $data['json'] = json_encode($matches);
    $this->load->view('admin/json_view', $data);

I've looked at a lot of posts on StackOverflow and via Google (hence the different output methods above) but nothing has seemed to solve the issue yet.

4

1 に答える 1

0

first you should try this:

  source: "<?php echo site_url(admin/lookup_tmdb_movie_titles); ?>"

then this could be:

function lookup_tmdb_movie_titles()
{
    $term = 'test';

    // TEST DATA
    $title['title'] = 'test';
    $title['label'] = 'test (2012)';
    $title['value'] = 'test';
    $title['year'] = '2012';
    $title['imdb_link'] = 'testlink';
    $matches[] = $title;

    // convert into JSON format and output
    $matches = array_slice($matches, 0, 5);
   var_dump($matches); //comment this if everythings ok
// echo json_encode($matches);  ->uncomment this if everythings ok

}

then open firebug launch the ajax request and check the response in console.

于 2012-12-26T23:14:39.690 に答える