函数调用后Ajax调用没有成功

时间:2012-09-24 17:10:51

标签: javascript ajax codeigniter

我正在尝试根据页面上的特定URI段加载动态数据。

继承我的Javascript:

$(document).ready(function() {
    $(function() {
        load_custom_topics();
    });

    $('#topics_form').submit(function() {
        var topic = document.getElementById('topics_filter').value
        $.ajax({
            type: "POST",
            url: 'ajax/update_session_topic',
            dataType: 'json', 
            data: { topic: topic },
                success: function(){
                    load_custom_topics()
                }
        });
        return false;
    });

    function load_custom_topics(){
        $.ajax({
        type: "POST",
        url: 'ajax/load_custom_topics',
        dataType: 'json',
        data: {},
                success: function (html) {
                    $('div.topics_filter').html(html['content']);
                    get_threads();
                }
        });
    }

    function get_threads(){
        var page = document.getElementById('page').value
        $.ajax({
        type: "POST",
        url: 'ajax/get_threads',
        dataType: 'json',
        data: {page: page},
            success: function (html) {
                $('div#thread_list').html(html['content']);
            }
        });
    }
});

因此,正如您所看到的,在页面加载时,它会启动load_custom_topics,它运行得很好。然后它应该调用get_threads()。这就是事情停止的地方,我没有数据。

Get_threads()

public function get_threads()
{
    $session = $this->session->userdata('active_topic');
    if ($this->input->post('page') == '1')
    {
        $data['list'] = $this->right_model->thread_list_all();
        $data['test'] = "all";
    } else {
        $data['list'] = $this->right_model->thread_list_other($session);
        $data['test'] = "not all";
    }
    if ($data['list'] == FALSE)
    {
        $content = "no hits";
    } else {
    $content = $this->load->view('right/thread_list', $data, TRUE);
    }
    $data['content'] = $content;
    $this->output->set_content_type('application/json')->set_output(json_encode($data));
}

当我动态创建“页面”时,HTML输出到:

<div name="page" value="1"></div>

get_threads()没有运行的原因是什么?

1 个答案:

答案 0 :(得分:2)

这没有ID。它有一个名字。

<div name="page" value="1"></div>

这意味着您对getElementById的请求失败。因此,这行代码应该在您的控制台中显示 TypeError

var page = document.getElementById('page').value