Jquery重定向不保存codeigniter会话

时间:2011-08-24 19:42:01

标签: codeigniter jquery codeigniter-2

我有一个注册页面,通过ajax请求我想将用户重定向到登录页面。但是,这个特定的ajax请求会执行重定向,但在POST请求之后不会传递会话

 var dataString = 'name='+ name + '&email=' + email1 + '&password=' + password1;
                       //alert (dataString);return false;
                       $.ajax({
                           type: "POST",
                           url: "http://localhost/ci/index.php/login/add/",
                           data: dataString,
                           success: function() {
                            window.location.href = "http://localhost/ci/dashboard";
                                                }
                       });

登录/添加功能通过POST请求获取值,然后将这些值放入会话并执行重定向。如果没有普通的php / CI中的ajax请求,同样的事情就会完美地发生。但是,不是通过ajax请求发生的。

这是PHP代码,如果它有帮助

    if ($this->input->post("add") == "Sign up") {
        $data_array = array(); //this array contains post data
        $output = $this->user_model->add_new($data_array);
        if($output == TRUE){
                $this->session->set_userdata('logged_in', '1'); 
        }

        $data = array(
                    'name' => form_error('name'), 
        'email' => form_error('email'), 
        'password' => form_error('password')    
        );
        echo json_encode($data);

}

1 个答案:

答案 0 :(得分:1)

The login/add function is taking the values via POST request which then puts these value in session and does a redirect. The same thing is happening perfectly without a ajax request in plain php/CI. But, not happening via ajax request.

因为之前你执行synchronous请求,而使用jQuery ajax你正在执行asynchronous请求(这就是说你的javascript可能会忽略你PHP脚本的某些部分)。尝试在ajax中添加async参数,并将其设置为false