我正在尝试从Openweather API获得响应,但是却收到404

时间:2018-11-05 06:19:13

标签: javascript angularjs

// MODULE
var weatherApp = angular.module('weatherApp', ['ngRoute', 'ngResource']);

// Directives
weatherApp.service('cityService', function() {
   this.city = "New York, NY";
});

//控制器

weatherApp.controller('homeController', ['$scope','cityService', function($scope, cityService){
      $scope.city = cityService.city;

      $scope.$watch('city', function(){
        cityService.city = $scope.city;
      });
}]);

  weatherApp.controller('forecastController', ['$scope', 'cityService', '$resource', function($scope, $resource, cityService){
          $scope.city = cityService.city;
     $scope.weatherApi = $resource('http://api.openweathermap.org/data/2.5/forecast', {
     callback: 'JSON_CALLBACK'
     }, {get: { method: 'JSONP'}});


         $scope.weatherResult = $scope.weatherApi.get({
         q: $scope.city, 
         cnt: 2,
        appid: 'b0a06997003bb34ff74635549a8bfd0e'
         });
     console.log($scope.weatherResult);
}]);

1 个答案:

答案 0 :(得分:1)

根据文档https://openweathermap.org/current#name,您不应传递州名,而只能传递城市名和可选的国家名。因此,http://api.openweathermap.org/data/2.5/forecast?appid=XXX&q=New%20York,%20US有效,而http://api.openweathermap.org/data/2.5/forecast?appid=XXX&q=New%20York,%20NY无效。有趣的是,http://api.openweathermap.org/data/2.5/forecast?appid=XXX&q=New%20York,%20NY,%20US也可以使用,尽管看起来好像没有记录。