我通过AJAX发布一个表单,如果成功,它将重定向到另一个页面。表单提交成功,但重定向部分失败。
在我的chrome开发者工具栏中,它显示了正确的请求url,请求方法为GET,状态代码为200OK
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>');
}
}
你知道我哪里错了吗?提前谢谢。
对于重定向,请尝试以下操作:
window.location = data.redirect;