0 votes
1 view
in Web Technology by (47.8k points)

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() {}

});

1 Answer

0 votes
by (107k points)

You can post data as form data instead of a request payload for that the below-mentioned line needed to be added to the $http object that is passed:

headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}

And the data that is being passed should be converted to a URL-encoded string by the following way:-

> $.param({fkey: "key"}) 

'fkey=key'

So at the whole, you need to do as follows:-

$http({

method: 'POST',

url: url,

data: $.param({fkey: "key"}),

headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}

})

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
+2 votes
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...