Синтаксическая ошибка в запросе openweathermap api - PullRequest
0 голосов
/ 30 сентября 2018

Я получаю следующую синтаксическую ошибку в консоли при попытке получить данные из 'openweathermap'

'Uncaught SyntaxError: Неожиданный токен:'

Вот файл js:

    var app = angular.module('App', ['ngResource']);
    app.factory('weatherService', function($http) {
      return {
        getWeather: function() {
          var weather = '';
        //  if (!!prmSearchValue) {
         //   var searchValue = prmSearchValue;
            $http.jsonp('https://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=c19bc0731cec50456576c7b36a675ca7&mode=json').success(function(data) {

                weather = 3232;

            });
        //  }
        /*   else {
            weather = {};
          } */
          return weather;

        }
      };
    });
    //Eilat,Israel
    app.controller('httpAppCtrlr', function($scope, weatherService) {
      $scope.searchText = '';
      $scope.searchWeather = function() {
        var prmSearchValue = $scope.searchText;
        $scope.weather = weatherService.getWeather();
      };



    });

Console

Похоже, что возвращаемые данные каким-то образом повреждены ..

Fiddle

Ответы [ 2 ]

0 голосов
/ 30 сентября 2018

В AngularJS jsonp необходимо добавить callback=JSON_CALLBACK к URL.(Я предполагаю, что есть причина, по которой вы используете jsonp вместо get.)

Замените

https://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=c19bc0731cec50456576c7b36a675ca7&mode=json

на

https://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=c19bc0731cec50456576c7b36a675ca7&mode=json&callback=JSON_CALLBACK

Fiddle

0 голосов
/ 30 сентября 2018

Используйте $http Get вместо JSONP.Лучший способ справиться с ошибкой - использовать .then. Измените свою фабрику следующим образом:

app.factory('weatherService', function ($http) {
    return {
        getWeather: function () {
            var weatherForcast = {};
            $http({
                method: 'GET',
                url: "https://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=c19bc0731cec50456576c7b36a675ca7"

            }).then(function successCallback(response) {
                angular.extend(weatherForcast, response.data);

            }, function errorCallback(response) {
                alert('API call failed, possibly due to rate limiting or bad zip code.');
            });
            return weatherForcast;
        }
    };
});

WORKING FIDDLE

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...