Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (47.6k 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 (106k 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
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...