将ajax数据传递给codeigniter控制器

时间:2012-05-07 22:14:54

标签: php ajax jquery-ui codeigniter

我试图通过AJAX从Jquery的可选功能传递数据并在codeigniter控制器中捕获它。没有运气!

的Javascript

$(function() {
    $( "#selectable" ).selectable({
        selected: updatefilters,
        unselected: updatefilters
    });   
    function updatefilters(ev, ui){
        var $selected = $('#selectable').children('.ui-selected');
        var filters = $selected.map(function(){return this.id;}).get().join("|");
        $.ajax({
            type: "POST",
            url: 'updatefilters',
            data: { filters: filters },
            success: function(data){
                $('#board').replaceWith(data.content);
            }
        });
    }
});

控制器

    public function updatefilters()
    {
        $filters = $this->input->post('data');
        print_r($filters);
        $filterarray = split("|", $filters);
        $this->thread_model->create_session_filter($filterarray); 
        $threads = $this->thread_model->get_threads();
        $data['json'] = '{"content":' + $threads + '}';
        $this->load->view('json_view', $data); // See step 4!!!
    }

浏览

json_view

<?php
header("Content-Type: application/json");
echo $json;
?>

jquery选择视图

<ol id="selectable">
    <li class="ui-state-default" id="everything">Everything!</li>
    <li class="ui-state-default" id="entertainment">Entertainment</li>
    <li class="ui-state-default" id="sci/tech">Sci/Tech</li>
    <li class="ui-state-default" id="news">News</li>
    <?php 

    if ($this->session->userdata('inactivefilter') == true) {
            $inactivefilter = $this->session->userdata('inactivefilter');
            foreach ($inactivefilter as $new)
                {
                    echo "<li class='ui-state-default' id='custom'>$new</li>";
                }
        }
    ?>
</ol>
在Firebug中,我可以看到通过AJAX传递的“过滤器”数据无法识别。

由于

1 个答案:

答案 0 :(得分:1)

试试这个:

   $.ajax({
        type: "POST",
        url: 'updatefilters',
        dataType: 'json',
        data: { filters: filters },
        success: function(data){
            $('#board').replaceWith(data.content);
        }
    });

我刚补充说: dataType:'json'

Greatings。