我的ajax电话第一次工作,但第二次工作不起作用,它返回类似的东西......你所要求的动作是不允许的。 在控制台的标题中,状态代码是403 Forbidden
控制器方法如下..
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 :(得分:1)
获取新的CSRF令牌。我们必须使用Javascript获取cookie,因为Codeigniter正在cookie中存储CSRF令牌。
将以下代码放在您的视图(.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>