gpt4 book ai didi

javascript - codeigniter ajax错误用户电子邮件检查

转载 作者:可可西里 更新时间:2023-10-31 22:47:48 26 4
gpt4 key购买 nike

我创建了一个简单的代码 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);
}
})
}
});
});

这是我的用户 Controller 功能,用于检查数据库中的电子邮件

    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 documentation :

error
Type: Function( jqXHR jqXHR, String textStatus, String errorThrown ) A function to be called if the request fails. The function receives three arguments: The jqXHR (in jQuery 1.4.x, XMLHttpRequest) object, a string describing the type of error that occurred and an optional exception object, if one occurred. Possible values for the second argument (besides null) are "timeout", "error", "abort", and "parsererror". When an HTTP error occurs, errorThrown receives the textual portion of the HTTP status, such as "Not Found" or "Internal Server Error." As of jQuery 1.5, the error setting can accept an array of functions. Each function will be called in turn. Note: This handler is not called for cross-domain script and cross-domain JSONP requests. This is an Ajax Event.

您使用 console.log 记录的是 jqXHR 参数。这应该有效:

error: function (jqXHR, errorType, error) {
$('#loading').hide();
console.log(errorType + ": " + error);
}

一旦您收到实际的错误消息,您就可以找到问题的根源。

关于javascript - codeigniter ajax错误用户电子邮件检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45115165/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com