[英]Getting response from Codeigniter controller method using AJAX/JQuery
[英]ajax call not getting the response from codeigniter controller
我正在嘗試使用jQuery和CodeIgniter驗證用戶登錄。 如果找到該用戶,他將被重定向到另一個頁面,所以我在這里嘗試的是在用戶鍵入錯誤的用戶名或密碼時得到錯誤消息。 那是我的主要目標。
在視圖中,我有以下代碼:
<?php
$param = 'id="formLogin"';
echo form_open('backOffice/loginCheck', $param);
?>
<label for="username">* user name</label>
<input type="text" name="username" id="username" />
<label for="password">* password</label>
<input type="password" name="password" id="password" />
<input type ="submit" name="login" id="login" value ="LOGIN" />
<?php echo form_close(); ?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"> </script>
<script src="<?php echo base_url(); ?>backOffice/js/loginscript.js"></script>
現在,這是loginscript.js中的代碼
btnLogin.on('click', function(errorMessage){
if(userName.val()!='' && password.val() !=''){
$.ajax({
type: 'POST',
url: 'backOffice/loginCheck',
data: 'formLogin.serialize()',
success: function(errorMessage){
console.log('i never get here');
},
dataType: 'jsonp'
});
e.preventDefault();
console.log(errorMessage);
}
})
最后,這是來自控制器的代碼
public function loginCheck()
{
// set the validation rules
$this->form_validation->set_rules('username', 'Username', 'required|trim|encode_php_tags');
$this->form_validation->set_rules('password', 'Password', 'required|trim|encode_php_tags');
$this->form_validation->set_error_delimiters('<br /><p class=jsdiserr>', '</p><br />');
if ($this->form_validation->run() != FALSE)
{
$ids=array();
$ids[0]=$this->db->where('username', $this->input->post('username'));
$ids[1] = $this->db->where('password', md5($this->input->post('password')));
$query = $this->backOfficeUsersModel->get();
if($query)
{
$data = array(
'username' => $this->input->post('username'),
'isUserLoggedIn' => true
);
$this->session->set_userdata($data);
$data['title'] = "Welcome to dashboard!";
$data['main_content'] = 'dashboard';
$this->load->vars($data);
$this->load->view('backOffice/template');
} else {
$errorMessage = "Wrong Username or Password";
$this->cms(json_encode($errorMessage));
}
} else {
// form validation fail, send the message
// form validation to fail, mean that user has javascript disabled
$errorMessage = "Wrong User Name OR Password.<br /> Please try again!";
$this->cms(json_encode($errorMessage));
}
} // end of function loginCheck
我猜data: 'formLogin.serialize()',
應該是data: formLogin.serialize(),
即formLogin
是將表單與您要發送的數據合並在一起的jQuery對象。 看來您的數據類型應該是json
而不是jsonp
另外,您將errorMessage
作為事件對象,但嘗試調用e.preventDefault()
您需要檢查的第一件事是顯示任何error code
或200
響應的ajax請求。
第二件事是btnLogin.on('click', function(errorMessage){
看起來不對,因為在這里errorMessage
可能充當event
。
第三件事在下面的代碼中, errorMessage
也用於成功響應。 建議您更改變量名稱。
btnLogin.on('click', function(errorMessage){
if(userName.val()!='' && password.val() !=''){
$.ajax({
type: 'POST',
url: 'backOffice/loginCheck',
data: 'formLogin.serialize()',
success: function(errorMessage){//here
console.log('i never get here');
},
dataType: 'jsonp'
});
e.preventDefault();
console.log(errorMessage);
}
})
第四件事是在loginCheck()
,您正在做以下事情
$this->load->vars($data);
$this->load->view('backOffice/template'); // both link will echo the html part
和
$errorMessage = "Wrong Username or Password";
$this->cms(json_encode($errorMessage));// json encode works for php array, not for string
所以根據我, loginCheck()
響應字符串而不是json。
最后在ajax()
添加error
函數,以便您也可以看到錯誤。 有關更多信息, 請檢查此
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.