Code Igniter Ajax post show 404

时间:2016-10-19 22:17:26

标签: javascript php jquery ajax codeigniter

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

parsererror SyntaxError:位于0(...)的JSON中的意外标记a

这是代码:

控制器

/**
 * 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 Post

$(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;
  });
});

2 个答案:

答案 0 :(得分:1)

这意味着您的控制器或模型包含语法错误或控制器或模型中的某处您回显/打印了一些您无法在ajax响应中处理的数据。

错误检查

1]首先检查不必要的回声/打印。

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

3]如果解决了,那么你的模型会出现问题,如果没有,那么你的控制器就会出现错误。

4]总是,通过检查获得AJAX呼叫的详细响应 检查元素&gt;网络&gt; XHR。 如果您的文件包含错误,则文件名将在XHR面板中以红色显示。 (在谷歌浏览器中)

我希望这能解决你的问题。

答案 1 :(得分:1)

我看到两个潜在的问题。第一个是控制器中的这一行。

echo $the_user_email;

这成为您回复的第一部分。它不是json,可能是罪魁祸首。

第二个问题

    $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']

模型将值返回到$email_exists,但您使用$if_email_exists来设置被编码为json响应的数组。也许我错过了一些东西,但我没有看到$if_email_exists在任何地方定义。

相关问题