简体   繁体   English

openweathermap API 请求中的语法错误

[英]Syntax error in openweathermap API request

I'm getting the following syntax error in the console while trying to get data from 'openweathermap'尝试从“openweathermap”获取数据时,我在控制台中收到以下语法错误

Uncaught SyntaxError: Unexpected token :未捕获的语法错误:意外标记:

Here is the JS file :这是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();
  };
});

安慰

It looks as if the data that returns is broken in some way..看起来好像返回的数据以某种方式被破坏了..

Fiddle小提琴

Use $http Get instead of JSONP.使用$http Get而不是 JSONP。 Better way to handle the error is using .then , Change your Factory as follows,处理错误的更好方法是使用.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

In AngularJS jsonp , you need to append callback=JSON_CALLBACK to the url.AngularJS jsonp ,您需要将callback=JSON_CALLBACK附加到 url。 (I'll assume there is a reason you're using jsonp instead of get .) (我假设您使用jsonp而不是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小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM