2012-07-19 90 views
1

Im通过AJAX发布表单,如果它成功,它将重定向到另一个页面。表单提交成功,但重定向部分失败。AJAX PHP重定向

在我的Chrome开发人员工具栏中,它显示正确的请求url,请求方法GET和状态码200 OK。

JavaScript的样子:

$('#submit-login').live('click', function(e) { 
    e.preventDefault(); 

    var username = $('#username').val(); 
    var password = $('#password').val(); 

    var dataString = 'username=' + username + '&password=' + password; 

    $.ajax({ 
     type: 'POST', 
     data: dataString, 
     url: '/account/do_ajax/login', 
     dataType: 'json', 
     success: function(data) { 
      if(data.redirect) { 
       window.location.href = data.redirect; 
      } else { 
       $('.message').hide(); 
       $('.message').html(data); 
       $('.message').fadeIn('slow'); 
      } 
     } 
    }); 
}); 

而且PHP

public function do_ajax($type) { 
    if($type == 'login') { 
     $this->_submit_login(); 
    } 
} 

    public function _submit_login() { 
    if($this->form_validation->run('login')) { 
     $data = array(
      'username' => $this->input->post('username'), 
      'password' => sha1($this->input->post('password')) 
     ); 
     $authenticate = $this->account_model->authenticate($data['username'], $data['password']); 
     if($authenticate) { 
      $this->session->set_flashdata('success', 'You have successfully logged in.'); 
      redirect('/account'); 
     } else { 
      echo json_encode('Could not log you in, your details are incorrect.'); 
     } 
    } else { 
     echo json_encode('<ul class="errors">'.validation_errors('<li>', '</li>').'</ul>'); 
    } 
} 

任何想法在那里我去错了吗?提前致谢。

回答

0

对于重定向试试这个:

window.location = data.redirect; 
+0

试过,没有运气 – 2012-07-19 20:21:46