[英]codeigniter ajax error user email check
我已經創建了一個簡單的代碼ajax請求,用於檢查codeigniter框架中的電子郵件可用性。 但是,我每次都會得到錯誤。 並且不知道如何解決它。 下面是我的頁腳js腳本(在jquery和其他外部腳本之后)。
<script>
$(document).ready(function() {
$("#loading").hide();
$("#email").blur(function() {
var email_val = $("#email").val();
var filter = /^[a-zA-Z0-9]+[a-zA-Z0-9_.-]+[a-zA-Z0-9_-]+@[a-zA-Z0-9]+[a-zA-Z0-9.-]+[a-zA-Z0-9]+.[a-z]{2,4}$/;
if (filter.test(email_val)) {
// show loader
$('#loading').show();
jQuery.ajax({
type: "POST",
url: "<?php echo site_url()?>users/check_email_ajax",
dataType: 'json',
data: {
'<?php echo $this->security->get_csrf_token_name(); ?>' : '<?php echo $this->security->get_csrf_hash(); ?>',
'email': 'email_val'
},
success: function (response) {
if(response){
$('#loading').hide();
console.log(response.message);
$('#msg').html(response.message)
$('#msg').show().delay(10000).fadeOut();
}
},
error: function(error){
$('#loading').hide();
console.log("There is some errors on server : " + error.error);
}
})
}
});
});
這是我的用戶控制器功能,用於檢查數據庫中的電子郵件
public function check_email_ajax(){
// allow only Ajax request
if($this->input->is_ajax_request()) {
// grab the email value from the post variable.
$email = $this->input->post('email');
// check in database - table name : users , Field name in the table : email
if(!$this->form_validation->is_unique($email, 'users.email')) {
// set the json object as output
$this->output->set_content_type('application/json')->set_output(json_encode(array('message' => 'The email is already taken, choose another one')));
}else{
$this->output->set_content_type('application/json')->set_output(json_encode(array('message' => 'you can use this email')));
}
}
}
在注冊表格中我有這個字段用於電子郵件輸入:
<div class="col-sm-5">
<input type="email" id="email" name="email" class="form-control">
<span id="loading"><img src="<?php echo base_url(); ?>ajax-loader.gif" alt="Ajax Indicator" /></span>
<div id="msg"></div>
</div>
但現在每次我改變輸入值。 我在console.log上收到錯誤
There is some errors on server : function (){if(c){var a=c.length;m(arguments),i?k=c.length:e&&e!==!0&&(j=a,n(e[0],e[1]))}return this}
有誰知道如何解決它?
您和成功調試之間的關系是這一部分:
error: function (error) {
$('#loading').hide();
console.log("There is some errors on server : " + error.error);
}
從jQuery文檔 :
錯誤
類型:Function(jqXHR jqXHR,String textStatus,String errorThrown)請求失敗時要調用的函數。 該函數接收三個參數:jqXHR(在jQuery 1.4.x,XMLHttpRequest中)對象,描述發生的錯誤類型的字符串和可選的異常對象(如果發生)。 第二個參數的可能值(除了null)是“timeout”,“error”,“abort”和“parsererror”。 發生HTTP錯誤時,errorThrown會收到HTTP狀態的文本部分,例如“Not Found”或“Internal Server Error”。 從jQuery 1.5開始,錯誤設置可以接受一系列函數。 每個函數將依次調用。 注意:不會為跨域腳本和跨域JSONP請求調用此處理程序。 這是一個Ajax事件。
您使用console.log
記錄的是jqXHR參數。 這應該工作:
error: function (jqXHR, errorType, error) {
$('#loading').hide();
console.log(errorType + ": " + error);
}
一旦得到實際的錯誤消息,您就可以找到問題的根源。
我之前做過這個練習。這是我使用的Ajax腳本。 我采取了不同的方法來獲取電子郵件。 我沒有放控制器代碼,因為它對你的控制器來說看起來更模糊。
$(document).ready(function() {
//Your Email on key press
$("#email").keyup(function() {
var email = $(this).val();
if (email.length > 3)
{
$.ajax({
type: 'POST',
url: emailAvailability_url,
data: {
email: email
}, success: function(data) {
// Your Ajax Resonse Here
} error: function (jqXHR, errorType, error) {
$('#loading').hide();
console.log(errorType + ": " + error);
}
});
}
});
});
這是我使用的控制器代碼:
public function check_email_availability(){
$data = $this->input->post();
$this->form_validation->set_error_delimiters('<span class="error">', '</div>');
if (($this->form_validation->run('email_verification') == FALSE)){
echo form_error('email');
} else {
unset($_POST);
echo '<span style="color:green;" id="emailIsValid">Available</span>';
}
}
我在配置中添加了驗證。
'email_verification' => array(
array(
'field' => 'email',
'label' => 'Email',
'rules' => 'required|max_length[50]|valid_email|is_unique[table_name.email]',
'errors' => array(
'required' => 'Email is required.',
'valid_email' => 'Plese Enter Valid Email',
'is_unique' => 'That Email is already taken. Try Again With New Email.'
)
)
)
答案的意圖只是盡力幫助。 因此,如果您想堅持自己的方法,那么這完全是您的選擇。 謝謝
我很抱歉,我已經找到了解決方案。 這是因為我在User控制器的構造函數中使用了Authentication。 我已將表單操作更改為另一個控制器並完成了。 現在我收到了電子郵件可用性消息,但是如果它在我什么都沒有之前拍攝 沒有ajax-loading gif顯示!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.