AngularJS $资源不发送X-Requested-With

我正在使用angular1.1.5和我正在使用$资源XHR到一个REST服务,但它似乎是美元资源不追加头X-Requested-With XMLHttpRequest, 是一个正常的行为? 我是否需要手动追加标题?

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); }; } 

只需将其添加到您的应用程序

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

它曾经但被改变了。 (看这里)

“X-Requested-With头文件在实践中很less使用,我们一直使用它来触发跨域请求的预检检查。”

从托马斯庞斯的答案在这里 。

我有同样的问题,我解决了它使用:

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

你也可以设置头来接受application/json

 $http({ method: 'GET', url: '/someUrl', headers: { Accept: 'application/json' } })