In the code below, the AngularJS $http
method calls the URL, and submits the xsrf object as a "Request Payload" (as described in the Chrome debugger network tab). The jQuery $.ajax
method does the same call, but submits xsrf as "Form Data".
How can I make AngularJS submit xsrf as form data instead of a request payload?
var url = 'http://somewhere.com/';
var xsrf = {fkey: 'xsrf key'};
$http({
method: 'POST',
url: url,
data: xsrf
}).success(function () {});
$.ajax({
type: 'POST',
url: url,
data: xsrf,
dataType: 'json',
success: function() {}
});
Just set Content-Type is not enough, url encode form data before send.
$http.post(url, jQuery.param(data))
I'm currently using the following solution I found in the AngularJS google group.
Note that if you're using PHP, you'll need to use something like Symfony 2 HTTP component's
Request::createFromGlobals()
to read this, as $_POST won't automatically loaded with it.The following line needs to be added to the $http object that is passed:
And the data passed should be converted to a URL-encoded string:
So you have something like:
From: https://groups.google.com/forum/#!msg/angular/5nAedJ1LyO0/4Vj_72EZcDsJ
UPDATE
To use new services added with AngularJS V1.4, see
I took a few of the other answers and made something a bit cleaner, put this
.config()
call on the end of your angular.module in your app.js:These answers look like insane overkill, sometimes, simple is just better:
If you do not want to use jQuery in the solution you could try this. Solution nabbed from here https://stackoverflow.com/a/1714899/1784301