2012-07-19 85 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