AngularJS – $http default JSON headers
The below JavaScript will change $http default headers to be JSON ready for SharePoint REST APIs. This is handy when sending GET/POST to SharePoint 2013 so we don’t need to repeat headers on each individual call. Enjoy! ![]()
//namespace
var namespace = namespace || {};
//NG-controller
namespace.myCtl = function ($scope, $mySvc) {
//viewmodel
var vm = $scope;
};
//NG-service
namespace.mySvc = function ($q, $http) {
$http.defaults.headers.common['Accept'] = 'application/json;odata=verbose';
//User Profile - me
this.getMe = function () {
return $http.get('/_api/SP.UserProfiles.PeopleManager/GetMyProperties');
};
//User Profile - users
this.getUser = function (login) {
var url = '/_api/SP.UserProfiles.PeopleManager/GetPropertiesFor(accountName=@v)?@v=\'domain\\' + login + '\'';
return $http.get(url);
};
};
//NG-module
angular.module('myApp', [])
.controller('myCtl', namespace.myCtl)
.service('$mySvc', namespace.mySvc)
.run(function () {
//prepare GUI
});
