If you want to define such a configuration at the 'app' level, ie. if
you're going to be expecting JSON from most/all of your
services/end-points, then using $httpProvider is the way to go, like you've
done.
You can always set headers for specific requests by using the $http service
directly instead of its shortcuts:
$http({
url: 'http://myservice.com/api/json-endpoint/',
method: 'GET',
params: {..},
headers: {
Accept: 'application/json'
}
}).success(function(data) {...});
On Monday, July 28, 2014 8:32:05 AM UTC+5:30, Christopher Hiller wrote:
>
> Sorry for my last message, it was incorrect. Use a config block:
>
> myApp.config(function($httpProvider) {
> $httpProvider.defaults.headers.get.Accept = 'application/json';
> });
>
> Should do what you want.
>
> On Sunday, July 27, 2014 5:42:18 PM UTC-7, Bruce Whealton wrote:
>>
>> Hello all,
>> What I am wanting to do, using the $http service, is to call a
>> Drupal based service, requesting JSON data in the response. I was looking
>> at the documentation and it was a bit confusing to me about where the
>> config object should be defined... I guess that is where you would do this,
>> right? I'll paste a snippet below and clarify what I want to do. I have a
>> controller defined in a controllers.js file. I want to replace the get
>> request that requests a local data.json file (or even a server based
>> data.json request) with a Drupal data source. My service will return
>> either xml or json. I want to tell my $http request to send the header
>> "Accept: application/json". Correct me if I am doing anything else wrong.
>> So here is my snippet:
>> var myApp = angular.module('myApp', []);
>>
>> myApp.controller('MyController', ['$scope', '$http', function($scope,
>> $http) {
>> $http.get('js/data.json').success(function(data) {
>> $scope.artists = data;
>> });
>> }]);
>>
>> So, instead of $http.get("js/data.json"), I want to request (
>> http://mydrupalsite.com/api/v1/node/) and include in the request the
>> statement that I want json data. The documentation says:
>>
>> The $http service will automatically add certain HTTP headers to all
>> requests. These defaults can be fully configured by accessing the
>> $httpProvider.defaults.headers configuration object, which currently
>> contains this default configuration:
>>
>> - $httpProvider.defaults.headers.common (headers that are common for
>> all requests):
>> - Accept: application/json, text/plain, * / *
>>
>>
>> So, would I add the $httpsProvider.defaults.headers = {"Accept":
>> "application/json"} just below the
>> var myApp = angular.module(''myApp', []); and before the controller
>> definition?
>> Or does it need to be inside the controller definition.
>> Thanks in advance for any help,
>> I hope I was clear,
>> Bruce
>>
>
--
You received this message because you are subscribed to the Google Groups
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.