Using Angular.js $ http.post with Codeigniter with CSRF Enabled

I am trying to send via Angular.js to a CodeIgniter controller in an application with CSRF enabled (in the same domain). With jquery, I would just add a token like this:

$.ajaxSetup({ data: { csrf_token: $("input:hidden[name='csrf_token']").val() } 

I added message values ​​and headers in Angular.js, but without joy. Does anyone know how to do this?

 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); }); 
+4
source share
2 answers

Here is the 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 details here: How to send data as form data instead of request payload?

+4
source

Try:

 var postData = { csrf_token: $("input:hidden[name='csrf_token]").val() }; 

This basically does the same as your jQuery ajax call. You can also get rid of headers in $http.post() .

0
source

Source: https://habr.com/ru/post/1485495/


All Articles