Ajax无法正常工作,在新网页中返回数组 笨

I am using codeigniter, the ajax is not redirecting to the home page, it redirect to a page contains this when trying to login with wrong pass=>

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

and when trying with a working password

{"st":1}

any one can help ? here it is the Ajax Code

<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>

signin controller

<?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"));
}}}

home controller

<?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);
}
}
?>

thanks in advance

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

or

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

before use base_url() load URL

$this->load->helper('url');
<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;   
});


});

I have changed something in js , please consider that

You can easily debug code and find fault.

  • first just alert('<?php echo site_url('home'); ?>'); in javascript and check url.
  • alert response for every cases.
  • use die("debug"); in index method of home controller.
  • Then check session data in different places.

I am sure you will got what fault you are making.

You are being redirect to that string because you have not stopped page submission. Note changes i made in form submit handler.

<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>