AngularJS $ resource does not send X-Requested-With

I am using angular 1.1.5 and I am using $ resource to do XHR for the REST service, but it looks like $ resource is not adding a header like X-Requested-With as XMLHttpRequest, is this normal behavior? and do i need to add the header manually?

function loginCtrl($scope,$resource) { $scope.submit = function () { var resource = $resource('/Api/User/login', {}, { authenticate: { method: 'POST', isArray: false, headers: { '__RequestVerificationToken': $scope.loginRequest.Token } } }); resource.authenticate($scope.loginRequest); }; } 
+44
angularjs
Aug 11 '13 at 4:46
source share
3 answers

Just add this to your app.

 myAppModule.config(['$httpProvider', function($httpProvider) { $httpProvider.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest'; }]); 
+101
May 27 '14 at 21:10
source share

It used to be, but it has been changed. (see here)

"X-Requested-With header is rarely used in practice and using it all the time when we run preflight checks for crossdomain requests."

From Thomas Pon's answer here .

+25
Sep 25 '13 at 13:19
source share

I had the same problem and decided to use it:

 myApp.config(['$httpProvider', function($httpProvider) { $httpProvider.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest'; }]); 

You can also set a header for accepting application/json :

 $http({ method: 'GET', url: '/someUrl', headers: { Accept: 'application/json' } }) 
+1
Mar 10 '16 at 15:11
source share



All Articles