2016-10-19 4 views
0

私はAJAXとPHPで検証しています.Gmailにログインしたときに電子メールを書いた後、その電子メールがレコードに存在するかどうかを検証します。コードイグナイターのフレームワークを使用していますが、私のコードのエラーは、私が電子メールデータを投稿するボタンをクリックしたときです:コードイグナイターAjaxポストショー404

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">[email protected]</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; 
    }); 
}); 

答えて

1

これは、コントローラまたはモデルにシンタックスエラーが含まれているか、ajaxレスポンスで処理できないデータをエコー/印刷しているコントローラまたはモデルのどこかにあることを意味します。不要なエコー/プリントの

エラーチェック

1]まずチェック。

2]モデル接続を削除して簡単なコントローラコードで試してください。

3]解決したら、モデルに何か問題があります。エラーがコントローラにない場合は、モデルに問題があります。

4] [要素の検査]> [ネットワーク]> [XHR]をチェックして、常にAJAXコールの詳細な応答を取得します。 ファイルにエラーがある場合、ファイル名はXHRパネルに赤色で表示されます。 (Google Chromeの場合)

これがあなたの問題を解決することを願っています。

+0

私のモデルではエラーでした。 –

+0

これらのMVCフレームワークでほとんどの時代が流れます:D –

1

2つの潜在的な問題があります。最初はコントローラのこの行です。

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に値を返しますが、あなたのJSONレスポンスにエンコードされます配列を設定する$if_email_existsを使用しています。多分私は何かを見逃してしまったが、$if_email_existsはどこにも定義されていない。

+0

$ if_email_exists ['']は変数です:$ email_exists、AJAXとCode Igniterで始まるエラーです。右を使って? –

+0

はい、 'datatype'が正しいと思われます。このオプションは、JQueryにサーバーから何を返すかを指示します。 'echo $ the_user_email;'の呼び出しを除いて、あなたはjsonを返しています。そのステートメントはデバッグのためのものだと思います。 – DFriend

関連する問題