不要使用jQuery
.ajax,而要使用$ http服务:
function getCurrentUserData() { var queryUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/SP.UserProfiles.PeopleManager/GetMyProperties"; var promise = $http({ url: queryUrl, method: "GET", headers: { "Accept": "application/json; odata=verbose" }, cache: false }).then(function(response) { return response.data; }).catch(function(response) { console.log("ERROR", response); throw response; }); return promise; }然后从返回的promise中提取数据:
function _init() { var promise = getCurrentUserData(); promise.then(function(data) { $scope.counter = data; console.log($scope.counter); }); }_init();$
http服务返回的promise已与AngularJS框架集成。只有在AngularJS执行上下文中应用的操作才能从AngularJS数据绑定,异常处理,属性监视等中受益。
有关更多信息,请参见
- AngularJS $ http服务API参考



