duanfa2014 2015-06-16 13:28
浏览 47

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

  • 写回答

4条回答 默认 最新

  • drgovyk64676 2015-06-16 13:43
    关注
    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');
    
    评论

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据