簡體   English   中英

openweathermap API 請求中的語法錯誤

[英]Syntax error in openweathermap API request

嘗試從“openweathermap”獲取數據時,我在控制台中收到以下語法錯誤

未捕獲的語法錯誤:意外標記:

這是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();
  };
});

安慰

看起來好像返回的數據以某種方式被破壞了..

小提琴

使用$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

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

小提琴

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM