javascript - Codeigniter Ajax 后显示 404

标签 javascript php jquery ajax codeigniter

我正在使用 AJAX 和 PHP 进行验证,我想做一些事情,比如当你登录 gmail 时,你写下你的电子邮件,然后程序验证该电子邮件是否存在于记录中,如果成功则显示用户名和电子邮件,否则程序必须说:该电子邮件不存在,我正在使用 Code Igniter 框架,当我单击按钮发布电子邮件数据时,我的代码中的错误是这样的:

解析器错误语法错误:JSON 中位置 0(…) 处出现意外标记 a

这是代码:

Controller

/**
 * Controller for email_validation 
 * 
 * @param the_user_email string
 * @return bool
 */
**dssad**
public function email_validation()
{
    if( $this->input->is_ajax_request() )
    {

    $the_user_email = $this->input->post('login_string');

    echo $the_user_email;

        $email_exists = $this->email_validation($the_user_email);

        if ($email_exists != FALSE) {

            echo json_encode([
                'status'   => 1,    
                'username' => $if_email_exists['username'],
                'email'    => $if_email_exists['email']
            ]);

        }
        else
        {
            echo json_encode([
                'status'   => 0,    
            ]);
        }

        return FALSE;
    }
    else
    {
        show_404();
    }
}

型号

 /**
 * Select a user email to validate
 *
 * @param  string  the user email to check
 * @return email if True
 * @return bool if False
 */
public function email_validation($the_user_email)
{
    $query = $this->db->select('username,email')
        ->from(config_item('user_table'))
        ->where('email', $the_user_email)
        ->limit(1)
        ->get();

    if( $query->num_rows() == 1 )
        return $query->row();

    return FALSE;
}

查看

     <center>
          <div class="row">
            <div class="col-md-12">

                <div class="row" id="login-card">
                    <div id="card-slide">
                        <div class="username-div">
                            <div id="user-img">
                                <img class="img-circle" src="img\lorem.jpg" alt="">
                            </div>
                            <div class="form-group">
                                <input type="email" class="form-control" name="login_string" id="login_string" placeholder="               Enter your email">
                                <div class="form-group"> </div>
                            </div>
                            <a class="btn-primary form-control" id="next-btn">Next</a>
                        </div>
                        <div class="pass-div">

                        <i id="arrow-left" class="fa fa-arrow-circle-left fa-2x" aria-hidden="true"></i>

                            <div id="user-img">
                                <img class="img-circle" src="" alt="">
                            </div>
                            <h1 id="Username_label">User Name</h1>
                            <label for="Username_label" id="Email_label">LoremIpsum@lorem.com</label>
                            <br><br>
                            <div class="form-group">
                                <input type="text" class="form-control" tabindex="-1" name="login_pass" placeholder="             Enter your password"  

                                 autocomplete="off" readonly="readonly" onfocus="this.removeAttribute('readonly');" />

                                <div class="form-group"> </div>
                            </div>                              

                            <button type="submit" class="btn btn-primary form-control" tabindex="-1" name="submit" id="submit_button">Sing In</button>

                        </div>
                    </div>

                </div>

                <p style="margin: 2em;">                        <!-- Forgot account -->
                    <a href="http://192.168.0.102/octopus/login/recover">
                        Can't access your account?
                    </a>
                </p>

            </div>
            </div>
        </center>

Ajax 帖子

$(document).ready(function(){
$('#next-btn').click(function(e){
    e.preventDefault();

    $.ajax({
        type: 'post',
        cache: false,
        url: '/octopus/login/email_validation',
        data: {
            'login_string': $('#login_string').val()
        },
        dataType: 'json',
        success: function(response){

            console.log(response);
            if(response.status == 1){
                $('#Username_label').replaceWith(response.username);
                $('#Email_label').replaceWith(response.email);
                $('#card-slide').addClass('show');
                $('#login-card').css('height','435px');
            }else if(response.status == 0){
                alert('This email does not exists.');
            }
        },
        error: function(jqXHR, textStatus, errorThrown) {
            console.log(textStatus, errorThrown);        
        }
    });
    return false;
  });
});

最佳答案

这意味着您的 Controller 或模型包含语法错误,或者在 Controller 或模型中的某个位置您正在回显/打印一些您无法在ajax响应中处理的数据。

错误检查

1] 首先检查是否有不必要的回显/打印。

2] 通过删除模型连接来尝试使用简单的 Controller 代码。

3] 如果解决了,那么您的模型有问题,如果没有解决,则错误出在您的 Controller 中。

4] 始终通过检查来获取 AJAX 调用的详细响应 检查元素 > 网络 > XHR。 如果您的文件包含错误,文件名将在 XHR 面板中以红色显示。 (在 Google Chrome 中)

希望这能解决您的问题。

关于javascript - Codeigniter Ajax 后显示 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40142421/

相关文章:

javascript - 使用 oninput 事件克隆 html 表单

JavaScript 井字棋计时器

php - Yii 中的 CMenu 样式

javascript - 滚动顶部重叠div

javascript - 无需身份验证的node.js SSL加密

javascript - 如何让jquery检测某个元素内的html元素?

php - 如何使用 PHP CURL 强制 XML 响应

php - Chrome 和 Safari 中的文本区域,所有可见的换行符都发送到服务器

javascript - 在下拉菜单中响应地隐藏和显示子元素

javascript - Angular Directive(指令)的行为不符合预期。我需要一些帮助来找出原因