Ajax 无法正常工作,在新网页中返回数组 |代码点火器


Ajax doesn't work properly, return array in a new webpage | Codeigniter

我正在使用codeigniter,ajax没有重定向到主页,当尝试使用错误的pass=登录时,它会重定向到包含此内容的页面>

{"st":0,"msg":"Invalid Username or Password"}

以及尝试使用有效密码时

{"st":1}

任何人都可以提供帮助?这里是阿贾克斯代码

<script type="text/javascript">
$(document).ready(function() {
$('#frm').submit(function(){
$.post(
     $('#frm').attr('action'), 
     $('#frm').serialize(), 
     function( data ) 
     {
     if (data.st == 0)
     {
     $('#validation-error').html(data.msg);
     }
           else if (data.st == 1)
     {
      window.location.href = <?php echo site_url('home'); ?>;
    }
     }, 
     'json'
   );
return false;   
});

});
</script>

登录控制器

<?php 
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class signin extends CI_Controller
{

function __construct(){
    parent::__construct();
              $this->load->library('form_validation');
$this->load->model('user_model');
}
public function index(){
$this->load->view('signin_view');
}
public function sayed(){echo "I love you";}
public function do_login() {
$this->form_validation->set_rules('username', 'Username',     'trim|required|xss_clean|min_length[4]|max_length[20]');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|min_length[4]|max_length[20]');
if ($this->form_validation->run() == FALSE) 
{
     echo json_encode(array('st'=>0, 'msg' => validation_errors()));
}
else {
$data = array(
'username' => $this->input->post('username'),
'password' => $this->input->post('password')
);
$result = $this->user_model->login($data);
if ($result == TRUE) {
$username = $this->input->post('username');
$result = $this->user_model->read_user_information($username);
if ($result != false) {
$session_data = array(
'username' => $result[0]->username,
'email' => $result[0]->email,   
);
$this->session->set_userdata('logged_in', $session_data);
     echo json_encode(array('st'=>1));
}
} else {
         echo json_encode(array('st'=>0, 'msg' => "Invalid Username or     Password"));
}}}

家用控制器

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Home extends CI_Controller{ 
function __construct(){
    parent::__construct();
}
public function index(){
            if($this->session->userdata('logged_in'))
{
 $this->load->view('home_view');
}
else
{
  redirect('login', 'refresh');
}

}
public function do_logout(){
$sess_array = array(
'username' => ''
);
$this->session->unset_userdata('logged_in', $sess_array);
redirect(signin);
}
}
?>

提前致谢

window.location.href = "<?php echo site_url('home'); ?>";

  window.location.href = "<?php echo base_url()."index.php/home" ?>";

使用前base_url()加载网址

$this->load->helper('url');

您可以轻松调试代码并查找错误。

  • 首先只需在JavaScript中alert('<?php echo site_url('home'); ?>');并检查URL。
  • 针对每个案例的警报响应。
  • 在家庭控制器的索引方法中使用die("debug");
  • 然后检查不同位置的会话数据。

我相信你会犯什么错。

您被重定向到该字符串,因为您尚未停止页面提交。请注意我在表单提交处理程序中所做的更改。

<script type="text/javascript">
$(document).ready(function() {
$('#frm').submit(function(event){
// Prevent page to submit.
event.preventDefault();
$.post(
     $('#frm').attr('action'), 
     $('#frm').serialize(), 
     function( data ) 
     {
     if (data.st == 0)
     {
     $('#validation-error').html(data.msg);
     }
           else if (data.st == 1)
     {
      window.location.href = <?php echo site_url('home'); ?>;
    }
     }, 
     'json'
   );
return false;   
});

});
</script>
<script type="text/javascript">
$(document).ready(function() {
$('#frm').submit(function(){
$.post(
 $('#frm').attr('action'), 
 $('#frm').serialize(), 
 function( data ) 
 {
var data = jQuery.parseJSON(data);

if (data.st == 0)
 {
 $('#validation-error').html(data.msg);
 }
       else if (data.st == 1)
 {
  window.location.href = <?php echo site_url('home'); ?>;
}
 }, 
 'json'
);
return false;   
});

});

我在js中更改了一些内容,请考虑