AngularJS在获取请求中包含API密钥

杰夫·塔巴尼克

我正在尝试使用AngularJS从Fantasy Data API获取信息。我正在使用$ resource在控制器中执行get请求,但无法弄清楚如何正确包含API密钥。我是否需要将其包括为标题?谢谢。

nflApp.controller('mainController', ['$scope','$resource','$routeParams', function($scope, $resource, $routeParams) {

$scope.fantasyAPI = $resource("https://api.fantasydata.net/nfl/v2/JSON/DailyFantasyPlayers/2015-DEC-28", { callback: "JSON_CALLBACK" }, { get: { method: "JSONP"}});

console.log($scope.fantasyAPI);

}]);

以下是该网站的http请求信息。 http请求信息

卢卡斯

您应该使用API​​密钥设置标头,在以下情况下,AngularJS将随每个请求发送给它们:

 $http.defaults.headers.common["Ocp-Apim-Subscription-Key"] = key;

当添加'.common'时,您要告诉angular在每个请求中发送此消息,因此您无需将其添加到每个访问API的资源中。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章