1

我试图通过 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 传递的“过滤器”数据未被识别。

谢谢

4

1 回答 1

1

尝试这个:

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

我刚刚添加:dataType:'json'

伟大的。

于 2012-05-07T22:20:46.857 回答