I'm trying to post via Angular.js to a CodeIgniter controller in an application that has CSRF enabled (on the same domain). With jquery, I would just add the token like so:
我试图通过Angular.js发布到启用了CSRF的应用程序中的CodeIgniter控制器(在同一个域上)。使用jquery,我会像这样添加令牌:
$.ajaxSetup({
data: {
csrf_token: $("input:hidden[name='csrf_token']").val()
}
I've added the post values and headers in Angular.js, but no joy. Anybody know how to accomplish this?
我在Angular.js中添加了帖子值和标题,但没有快乐。谁知道怎么做到这一点?
var postData = { name:"csrf_token", value: $("input:hidden[name='csrf_token]").val() };
$http.post('myCIcontroller',postData,{headers: {'csrf_token': $("input:hidden[name='csrf_token']").val()}}).success(function(data){
console.log(data);
});
2 个解决方案
#1
4
Here is a solution:
这是一个解决方案:
var postData = $.param({csrf_token: $("input:hidden[name='csrf_token']").val()});
$http.post('myCIcontroller', postData, {headers : {'Content-Type': 'application/x-www-form-urlencoded'}}).success(function(data){
console.log(data);
});
More info here: How can I post data as form data instead of a request payload?
更多信息:如何将数据作为表单数据而不是请求有效负载发布?
#2
0
Try:
尝试:
var postData = { csrf_token: $("input:hidden[name='csrf_token]").val() };
This basically does the same as your jquery ajax call. You might also want to get rid of the headers
in $http.post()
.
这基本上和你的jquery ajax调用一样。您可能还想删除$ http.post()中的标头。
#1
4
Here is a solution:
这是一个解决方案:
var postData = $.param({csrf_token: $("input:hidden[name='csrf_token']").val()});
$http.post('myCIcontroller', postData, {headers : {'Content-Type': 'application/x-www-form-urlencoded'}}).success(function(data){
console.log(data);
});
More info here: How can I post data as form data instead of a request payload?
更多信息:如何将数据作为表单数据而不是请求有效负载发布?
#2
0
Try:
尝试:
var postData = { csrf_token: $("input:hidden[name='csrf_token]").val() };
This basically does the same as your jquery ajax call. You might also want to get rid of the headers
in $http.post()
.
这基本上和你的jquery ajax调用一样。您可能还想删除$ http.post()中的标头。