Codeigniter带有ajax请求的csrf令牌(500个内部服务器错误) [英] Codeigniter csrf token with ajax request (500 internal server error)
问题描述
我很难通过我的CI表单使用csrf令牌来尝试发出ajax请求。我一直在做一个漫长的研究,我想出了相同的解决方案发布在每一个相关的问题,这是添加令牌val到ajax请求中的序列化数据。我在我的ajaxSetup,我得到的令牌,但仍然遇到相同的问题。这是我的代码。
// AJAX设置
$ .ajaxSetup({
数据:{
csrf_test_name:$(input [name ='csrf_test_name'])。 val()
}
});
//函数ajax登录
$(form#login)。on(submit,function(e){
var $ this = $(this);
var mensaje = $(div.msglogin);
$ .ajax({
type:POST ,
url:$ this.attr(action),
data:$ this.serialize(),
beforeSend:function(){
mensaje.html ; p>< img src =public / frontend / img / miniloader.gif>< span>< small> Iniciando ..< / small>< / span>< / p&
}
})
.done(function(data){
console.log($ this.serialize());
if ==redirect){
window.location.replace($(input#baselogin)。val());
} else {
mensaje.html(data);
}
})
e.preventDefault();
});
这是我在console.log $ this.serialize()时得到的,正在发送
csrf_test_name = 4a4d6eb47fc8f0c8e932b3b56a4eb9c5& usuario = dan& password = meaannn
解决方案添加CSRF标记到发布之前的
数据
选项:$ .ajax
type:POST,
url:$ this.attr(action),
data:{'<??php echo $ this-> security-> get_csrf_token_name );?>':'<?php echo $ this-> security-> get_csrf_hash();?>',/ * .... your data .... * /},
beforeSend:function(){
mensaje.html('< p>< img src =public / frontend / img / miniloader.gif>< span>< small> Iniciando ..< ; / small>< / span>< / p>');
}
})
CSRF令牌需要与每个请求一起发送,因此应由数据指定。
echo
语句执行此操作。您可以单独添加和序列化,但我已经显示了您缺少的。I'm having a hard time trying to make an ajax request through my CI form having the csrf token enabled. I've been doing a long research and I came up with the same solution is posted in every issue related with this one which is adding the token val to the serialized data in the ajax request. I did this in my ajaxSetup, I get the token but still experiencing the same issue.. Here is my code.
//AJAX Setup $.ajaxSetup({ data:{ csrf_test_name: $("input[name='csrf_test_name']").val() } }); //Function ajax login $("form#login").on("submit", function(e){ var $this = $(this); var mensaje = $("div.msglogin"); $.ajax({ type: "POST", url: $this.attr("action"), data: $this.serialize(), beforeSend: function() { mensaje.html('<p><img src="public/frontend/img/miniloader.gif"><span><small> Iniciando..</small></span></p>'); } }) .done (function(data){ console.log($this.serialize()); if(data == "redirect"){ window.location.replace($("input#baselogin").val()); }else{ mensaje.html(data); } }) e.preventDefault(); });
This is what I get when I console.log $this.serialize() which means the token is being send
csrf_test_name=4a4d6eb47fc8f0c8e932b3b56a4eb9c5&usuario=dan&password=meaannn
Any help will be appreciated.
解决方案Add the CSRF token to your
data
option before posting:$.ajax({ type: "POST", url: $this.attr("action"), data: {'<?php echo $this->security->get_csrf_token_name(); ?>':'<?php echo $this->security->get_csrf_hash(); ?>',/*....your data....*/}, beforeSend: function() { mensaje.html('<p><img src="public/frontend/img/miniloader.gif"><span><small> Iniciando..</small></span></p>'); } })
The CSRF token needs to be sent with each request so it should be specified by the data. The
echo
statement does this. You can add this separately and serialize, but I've shown what you are missing.这篇关于Codeigniter带有ajax请求的csrf令牌(500个内部服务器错误)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!