JQuery脚本无法正确加载

时间:2012-01-22 15:33:56

标签: jquery codeigniter

我有一个名为Profile的控制器,其中包含get_user_timelinedo_upload等多项功能。使用来自JQuery的ajax调用将get_user_timeline加载到profile_view。它的功能是每隔60秒拉一下推特时间线。

我将一个表单放入视图以调用do_upload函数后运行良好。 do_upload功能是将图片或mp3等文件上传到服务器。我将表单验证放入该函数中,因此当用户未填写整个函数时,他/她将重定向回profile_view并发出警告。我是通过在$this->index()函数中调用do_upload来完成的。

当我测试我的代码时,我提交了几个输入留空的表单。 profile_view加载了良好的表单验证错误警告,但似乎我的Jquery将不再工作,因为它不显示twitter时间轴。我检查了网址..而是http://localhost/myproject/profile/do_upload而不是http://localhost/myproject/profile。是CI默认行为吗?怎么处理呢?似乎我的Jquery只适用于http://localhost/myproject/profile你能给出任何建议吗?谢谢

这是我的代码。 配置文件控制器

<?php

class Profile extends CI_Controller
{
    function __construct()
    {
        parent::__construct();

        $this->load->library('tweet');
        $this->load->library('form_validation');
        $this->load->model('user_model');

    }

    function index()
    {
        if($this->session->userdata('is_logged_in') == TRUE)
        {

            $data['biography'] = $this->user_model->get_user_by_id($this->session->userdata('user_id'));


            $data['user'] = $this->tweet->call('get', 'users/show', array('id' => $this->session->userdata('user_id')));

            $data['main_content'] = 'private_profile_view';
            $this->load->view('includes/template', $data);
        }
        else
        {

            redirect('welcome');
        }
    }


    /** timeline features **/
    function get_home_timeline()
    {
        $timeline = $this->tweet->call('get', 'statuses/home_timeline');
        echo json_encode($timeline);

    }


//the do_upload function
function do_upload()
    {
        $config['upload_path'] = './media/';
        $config['allowed_types'] = 'mp3';
        $config['max_size'] = '30720';

        $this->load->library('upload', $config);
        //set validation rules
        $this->form_validation->set_rules('title', 'Song title', 'trim|required|xss_clean');
        $this->form_validation->set_rules('album', 'Album', 'trim|required|xss_clean');
        $this->form_validation->set_rules('artist', 'Artist', 'required|xss_clean');
        $this->form_validation->set_rules('song_license_agreement', 'License', 'required|xss_clean');

        if ($this->form_validation->run() == FALSE)
        {
            $this->index(); //this return the warning but make my Jquery fail
        }
    }   

THE .JS

$(document).ready(function(){


                update_timeline('profile/get_home_timeline', '#home_timeline ul', 'home'); //this is call to the timeline, this won't work if i submit the upload form

});

function update_timeline()
{
   //my ajax call goes here
}

观点

<div id="timeline" class="">
            <ul>
                <li><a href="#home_timeline" id="home_timeline_menu">Home</a></li>
                <li><a href="#user_timeline" id="user_timeline_menu">MyStatus</a></li>
                <li><a href="#mentions_timeline" id="mentions_timeline_menu">@me</a></li>
            </ul>
            <section id="home_timeline">
                <ul>
                   <!-- this is where the timeline goes -->
                </ul>
            </section>
<div>
<!-- and this is the form -->
<article id="music_upload_form">

                <?php echo validation_errors(); ?>
                <?php echo form_open_multipart('profile/do_upload');?>
                <input type="hidden" id="user_id" name="user_id" value="<?php echo $user->id_str;?>" />
                <label for="title">Song Title</label>
                <input type="text" name="title" id="title" size="30" value="<?php echo set_value('title'); ?>"/><br />
                <label for="album"> Album Title </label>
                <input type="text" name="album" id="album" size="30" value="<?php echo set_value('album'); ?>"/><br />
                <label for="artist">Artist</label>
                <input type="text" name="artist" id="artist" size="20" value="<?php echo set_value('artist'); ?>"/><br />
                <input type="file" name="userfile" size="20" /><br />
                <input type="radio" name="owner" id="license" value="<?php echo set_value('owner'); ?>"/>I AM the artist/owner of this song and I have the right for distribution<br />
                <input type="radio" name="licensed_user" id="license" value="<?php echo set_value('licensed_user');?>"/>I AM NOT the artist/owner of this song BUT I have the right for distribution</br>
                <input type="checkbox" name="song_license_agreement" value="<?php echo set_value('song_license_agreement');?>"/>By selecting this option, I swear that all information entered is right<br />
                <input type="submit" value="Upload Your Song" />
                </form>
            </article>

1 个答案:

答案 0 :(得分:1)

好的,我我得到了正在发生的事情。

而不是调用仅在当前版本中运行$this->index()方法的index(),而是重定向用户。

function do_upload()
{
    if ($error)
    {
        redirect('profile');
    }
}

调用$this->index()实际上并不会更改URL或当前方法,它只会运行您拥有的任何代码。

要使错误显示在下一页上,您可以将它们分配给会话项:

$this->session->set_flashdata('errors', validation_errors());

然后在你看来:

echo $this->session->flashdata('errors');

我使用了flashdata,因为它会在下次请求时自动清除。您可能希望考虑编写或采用某种反馈/消息库。