javascriptangularjsjsonpopenweathermapangularjs-factory

Syntax error in openweathermap API request


I'm getting the following syntax error in the console while trying to get data from 'openweathermap'

Uncaught SyntaxError: Unexpected token :

Here is the JS file :

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

It looks as if the data that returns is broken in some way..

Fiddle


Solution

  • In AngularJS jsonp, you need to append callback=JSON_CALLBACK to the url. (I'll assume there is a reason you're using jsonp instead of get.)

    Replace

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

    with

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

    Fiddle