0

我有一个注册页面,我想通过 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);

}

4

1 回答 1

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 脚本的某些部分)。尝试async在您的ajax中添加参数并将其设置为false

于 2011-08-24T20:48:38.573 回答