Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 3a75b11

Browse files
rkirovIgorMinar
authored andcommitted
fix($http): remove 'X-Requested-With' from header defaults
X-Requested-With header is rarely used in practice and by using it all the time we are triggering preflight checks for crossdomain requests. We could try detecting if we are doing CORS requests or not, but it doesn't look like it's worth the trouble. BREAKING CHANGE: X-Requested-With header is not set by $http service any more. If anyone actually uses this header it's quite easy to add it back via: ``` myAppModule.config(['$httpProvider', function($httpProvider) { $httpProvider.defaults.headers.common["X-Requested-With"] = 'XMLHttpRequest'; }]); ``` Closes #1004
1 parent a32bc40 commit 3a75b11

File tree

2 files changed

+3
-10
lines changed

2 files changed

+3
-10
lines changed

src/ng/http.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,7 @@ function $HttpProvider() {
108108
// default headers
109109
headers: {
110110
common: {
111-
'Accept': 'application/json, text/plain, */*',
112-
'X-Requested-With': 'XMLHttpRequest'
111+
'Accept': 'application/json, text/plain, */*'
113112
},
114113
post: {'Content-Type': 'application/json;charset=utf-8'},
115114
put: {'Content-Type': 'application/json;charset=utf-8'}
@@ -212,7 +211,6 @@ function $HttpProvider() {
212211
*
213212
* - `$httpProvider.defaults.headers.common` (headers that are common for all requests):
214213
* - `Accept: application/json, text/plain, * / *`
215-
* - `X-Requested-With: XMLHttpRequest`
216214
* - `$httpProvider.defaults.headers.post`: (header defaults for HTTP POST requests)
217215
* - `Content-Type: application/json`
218216
* - `$httpProvider.defaults.headers.put` (header defaults for HTTP PUT requests)

test/ng/httpSpec.js

+2-7
Original file line numberDiff line numberDiff line change
@@ -377,8 +377,7 @@ describe('$http', function() {
377377

378378
it('should set default headers for GET request', function() {
379379
$httpBackend.expect('GET', '/url', undefined, function(headers) {
380-
return headers['Accept'] == 'application/json, text/plain, */*' &&
381-
headers['X-Requested-With'] == 'XMLHttpRequest';
380+
return headers['Accept'] == 'application/json, text/plain, */*';
382381
}).respond('');
383382

384383
$http({url: '/url', method: 'GET', headers: {}});
@@ -389,7 +388,6 @@ describe('$http', function() {
389388
it('should set default headers for POST request', function() {
390389
$httpBackend.expect('POST', '/url', 'messageBody', function(headers) {
391390
return headers['Accept'] == 'application/json, text/plain, */*' &&
392-
headers['X-Requested-With'] == 'XMLHttpRequest' &&
393391
headers['Content-Type'] == 'application/json;charset=utf-8';
394392
}).respond('');
395393

@@ -401,7 +399,6 @@ describe('$http', function() {
401399
it('should set default headers for PUT request', function() {
402400
$httpBackend.expect('PUT', '/url', 'messageBody', function(headers) {
403401
return headers['Accept'] == 'application/json, text/plain, */*' &&
404-
headers['X-Requested-With'] == 'XMLHttpRequest' &&
405402
headers['Content-Type'] == 'application/json;charset=utf-8';
406403
}).respond('');
407404

@@ -412,8 +409,7 @@ describe('$http', function() {
412409

413410
it('should set default headers for custom HTTP method', function() {
414411
$httpBackend.expect('FOO', '/url', undefined, function(headers) {
415-
return headers['Accept'] == 'application/json, text/plain, */*' &&
416-
headers['X-Requested-With'] == 'XMLHttpRequest';
412+
return headers['Accept'] == 'application/json, text/plain, */*';
417413
}).respond('');
418414

419415
$http({url: '/url', method: 'FOO', headers: {}});
@@ -424,7 +420,6 @@ describe('$http', function() {
424420
it('should override default headers with custom', function() {
425421
$httpBackend.expect('POST', '/url', 'messageBody', function(headers) {
426422
return headers['Accept'] == 'Rewritten' &&
427-
headers['X-Requested-With'] == 'XMLHttpRequest' &&
428423
headers['Content-Type'] == 'Rewritten';
429424
}).respond('');
430425

0 commit comments

Comments
 (0)