AngularJS version 1.1 HttpProvider common header change breaks Rails

836 views
Skip to first unread message

Daniel Nelson

unread,
Feb 26, 2013, 7:01:44 PM2/26/13
to ang...@googlegroups.com
AngularJS used to work out of the box with Rails. Today we discovered
that a v1.1 change in the HttpProviders common headers breaks the
ability to work out of the box with Rails. It is easy to fix once you
know what to look for, but it can make it very frustrating trying to
get up and running with AngularJS + Rails. It is up to the core
AngularJS team of course, but I wanted to make sure that the
implications of this change are understood with respect to ease of
adopting AngularJS within the Rails community.

In 1.0, $HttpProvider had the following common headers:
common: {
'Accept': 'application/json, text/plain, */*',
'X-Requested-With': 'XMLHttpRequest'
}

In 1.1, it has:
common: {
'Accept': 'application/json, text/plain, */*'
}

The breakdown in Rails occurs because the Rails request object looks
first for a format extension on the URL (convention is not to include
this), then for what it considers to be a valid Accept header (eg:
'application/json'; it doesn't like multiple Accept definitions such
as 'application/json, text/plain, */*'), and then checks whether the
request was made via XHR (it looks for X-Requested-With of
XMLHttpRequest). Since the X-Requested-With header has been removed in
AngularJS 1.1, Rails now things that requests coming from Angular are
requesting a format of text/html.

This can be fixed in an Angular module by updating the default HTTP headers:

analyticsModule.config ['$httpProvider', ($httpProvider) ->
$httpProvider.defaults.headers['common']['Accept'] = 'application/json'
]

Or:

analyticsModule.config ['$httpProvider', ($httpProvider) ->
$httpProvider.defaults.headers['common']['X-Requested-With'] =
'XMLHttpRequest'
]

But it would be preferable to me if the previous AngularJS behavior
were restored or the default Accept header were reduced to
application/json.

Thank you for your consideration,

Daniel

ceej

unread,
Apr 7, 2013, 8:58:51 PM4/7/13
to ang...@googlegroups.com
I have the same issue with 1.1.4

I had to add:

analyticsModule.config ['$httpProvider', ($httpProvider) -> 
  $httpProvider.defaults.headers['common']['X-Requested-With'] = 'XMLHttpRequest' 

To get things to function as normal.  Thanks to Daniel.
Reply all
Reply to author
Forward
0 new messages