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

Posted

技术标签:

【中文标题】codeigniter ajax 错误用户电子邮件检查【英文标题】:codeigniter ajax error user email check 【发布时间】:2017-12-20 06:35:49 【问题描述】:

我创建了一个简单的代码 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"  /></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

有人知道怎么解决吗?

【问题讨论】:

请打开您的 Web 控制台并添加由您的 ajax 函数发布的请求标头/响应和 JSON 获取的屏幕截图 另外,你能不能只添加console.log(error)的输出; 【参考方案1】:

你和成功调试之间的障碍是这部分:

error: function (error) 
    $('#loading').hide();
    console.log("There is some errors on server : " + error.error);

来自jQuery documentation:

错误 类型:函数(jqXHR jqXHR,字符串 textStatus,字符串 errorThrown ) 请求失败时调用的函数。该函数接收三个参数:jqXHR(在 jQuery 1.4.x 中,XMLHttpRequest)对象,一个描述发生的错误类型的字符串和一个可选的异常对象(如果发生)。第二个参数(除了 null)的可能值是“timeout”、“error”、“abort”和“parsererror”。发生 HTTP 错误时,errorThrown 会接收 HTTP 状态的文本部分,例如“未找到”或“内部服务器错误”。从 jQuery 1.5 开始,错误设置可以接受一个函数数组。每个函数都会被依次调用。注意:跨域脚本和跨域 JSONP 请求不调用此处理程序。这是一个 Ajax 事件。

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

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

一旦你得到实际的错误信息,你就可以找到问题的根源。

【讨论】:

那很快。干得好! 感谢@AlexanderHiggins 先生的回答:我已经更改了与您的评论类似的功能,但出现此错误。 parsererror: SyntaxError: Unexpected token 【参考方案2】:

我以前做过这个练习,所以这是我使用的 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.'
                    )
            )
)

回答的目的只是试图提供帮助。因此,如果您想坚持自己的方法,那完全是您的选择。 谢谢

【讨论】:

我想检查是否正确输入了电子邮件 EX:me@example.com 但我认为您只检查了输入的长度。反正 。感谢您的回复 不,先生,您弄错了,我采取了email.length &gt; 3 只是因为我想检查用户何时添加某些内容(我已经使用了keyup 所以)然后我将其发送到控制器并在控制器端我使用 CI 验证来检查电子邮件是否唯一。在 ajax 响应中,我会得到与之相关的消息。【参考方案3】:

很抱歉,我已经找到了解决方案。这是因为我在用户控制器的构造函数中使用了身份验证。我已将表单操作更改为另一个控制器并完成。 现在我收到了电子邮件可用性消息,但如果它在我一无所获之前就被采取了。没有显示 ajax 加载 gif !

【讨论】:

以上是关于codeigniter ajax 错误用户电子邮件检查的主要内容,如果未能解决你的问题,请参考以下文章

Codeigniter 抛出错误并且不向用户发送确认邮件

CodeIgniter form_validation 未显示错误

CodeIgniter:我在提交注册表时出现此数据库错误 1048。

Codeigniter 3 无法访问错误消息

通过ajax发送数据时Codeigniter 403禁止错误

为啥我通过 jquery ajax 和 codeigniter 得到验证错误?