Daniel Nelson
unread,Feb 26, 2013, 7:01:44 PM2/26/13Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
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