2017-12-16 15 views
2

私のajax呼び出しは初めて動作しましたが、2度目には動作しませんでした..あなたが要求した動作は許可されません。ステータスコードは以下のようにAJAXはcodeigniterで2回目のcsrfトークンの問題を処理しません

コントローラ方法403禁止されているコンソールのヘッダに ..

public function authenticate() { 
    echo json_encode('sachin'); 
} 

jQueryの機能

$("#adminFrmSubmitBtn").on('click',function(){ 
    if($("#admin_login_frm").valid()){ 
     var frmData=$(this).closest('form').serialize(); 
     frmData+='&'+$.param({'<?php echo $this->security->get_csrf_token_name(); ?>' : '<?php echo $this->security->get_csrf_hash(); ?>'}); 
     $.ajax({ 
      url:'<?php echo base_url('login/authenticate12'); ?>',//$(this).closest('form').attr('action'), 
      method:$(this).closest('form').attr('method'), 
      data:frmData, 
      success:function(response){ 
       console.log(response); 
      } 
     }); 
    }else{ 
     return false; 
    } 
}); 

のHTMLコード

<form action="<?php echo base_url('login/authenticate'); ?>" method="post" name="admin_login_frm" id="admin_login_frm">    
     <input type="text" name="user_name" class="form-control" placeholder="Email"> 
     <input type="password" name="user_password" class="form-control" placeholder="Password"> 
     <button type="button" class="btn btn-primary btn-block btn-flat" > 
</form> 
+0

'$ config ['csrf_regenerate']'の設定値は何ですか? –

+0

CSRFで何をアーカイブしようとしていますか? –

+0

[このフォーラムの回答は、あなたのjsonレスポンスとともに新しいCSRFトークンを返信し、次のajaxコールのCSRFとユーザ名の隠しフィールドを更新することを示唆しています。](https://forum.codeigniter.com/thread-61278-post-327081) .html#pid327081) – ourmandave

答えて

1

新しいCSRFトークンを取得するにはCodeigniterはCookieにCSRFトークンを格納しているので、Javascriptを使用してCookieを取得する必要があります。

ビュー(.php)ファイルに次のコードを挿入します。

<script type="text/javascript"> 

    function get_cookie(cname) { //using this function we can get cookie 
     var name = cname + "="; 
     var ca = document.cookie.split(';'); 
     for(var i=0; i<ca.length; i++) { 
      var c = ca[i]; 
      while (c.charAt(0)==' ') c = c.substring(1); 
      if (c.indexOf(name) == 0) return c.substring(name.length,c.length); 
     } 
     return ""; 
    } 

    $.ajax({ 
     type: "POST", 
     url: "YOUR-URL", 
     data: {"<?php echo $this->security->get_csrf_token_name(); ?>":csrf_token}, //Add your data which you want to post 
     dataType: "json", 
     success: function(resp){    
     }, 
     error: function(jqXHR, textStatus, errorThrown){ 
      alert('Something is wrong'); 
      window.location.reload(); 
     } 
    }); 

</script> 
関連する問題