我正在尝试使用openweathermap.org的外部API来获取天气预报,并且到目前为止我想使用angular.js来获得单个城市的预报,而不用API键来获得多个城市的预报,因为我无法找到使用api键进行预报的正确方法。
下面是我想要做的事情的清晰图片
1.应用程式应接受来自使用者2的多个城市名称。根据输入的城市名称,应用程式应显示每个城市14天的天气预报。
var artistControllers = angular.module('artistControllers', []);
var apiKey = '89b59e4d7d07894243b5acd24e7f18a3';
artistControllers.controller('WController', ['$scope', '$http', function($scope, $http) {
console.log('ijij');
$http.jsonp('api.openweathermap.org/data/2.5/forecast/daily?id=524901').success(function(err,data){
if(err){
console.log(err);
}
$scope.data=data;
console.log(data);
});
}]);发布于 2015-02-02 18:46:28
文件中写着:
如何使用API键 将以下参数添加到GET请求中:
APPID=APIKEY示例:api.openweathermap.org/data/2.5/forecast/city?id=524901&APPID=1111111111或向服务器添加以下行到请求的http头:x-api-key:APIKEY
因此,您应该可以在$http调用的url后面附加API键。
请参阅:http://openweathermap.org/appid
或者,如文档所述,您可以在HTTP请求中添加额外的标头。您可以在AngularJS中这样做:
var req = {
method: 'GET',
url: 'api.openweathermap.org/data/2.5/forecast/daily?id=524901',
headers: {
'x-api-key': '89b59e4d7d07894243b5acd24e7f18a3'
}
}
$http(req).success(function(){...}).error(function(){...});https://stackoverflow.com/questions/28282894
复制相似问题