worldweatheronline API响应错误

worldweatheronline API response error

本文关键字:错误 响应 API worldweatheronline      更新时间:2023-09-26

我正在尝试从'worldweatheronline'中使用$http调用天气信息:

this.testApi = function(coords) {
        var deferred = $q.defer();
        $http.jsonp(API_ROOTS + '?key=9834687w634087623eg8932te&q=' + coords.latitude + ',' + coords.longitude + '&cc=yes&includeLocation=yes&format=json')
        .then(function(response) {
          deferred.resolve(response.current_condition);
          console.log(response.current_condition);
        }, function(error) {
            deferred.reject(error);
        }
        );
        return deferred.promise;
    };

和控制器:

$scope.updateLocalWeather = function() {
      $geolocation.get().then(
        function(position) {
          $weather.testApi(position.coords).then(
            function(weather) {
              $scope.localWeather = weather;
                $ionicSlideBoxDelegate.update();
            }
          );
        }
      );
    };

,从控制台中显示一个错误:

Uncaught SyntaxError: Unexpected token :

,但是当我输入console.log:

时,响应通过了
{ "data": { "current_condition": [ {"cloudcover": "0", "FeelsLikeC": "11", "FeelsLikeF": "51", "humidity": "42", "observation_time": "07:29 AM", "precipMM": "0.0", "pressure": "1029", "temp_C": "11", "temp_F": "51", "visibility": "10", "weatherCode": "113",  "weatherDesc": [ {"value": "Sunny" } ],  "weatherIconUrl": [ {"value": "http:'/'/cdn.worldweatheronline.net'/images'/wsymbols01_png_64'/wsymbol_0001_sunny.png" } ], "winddir16Point": "SW", "winddirDegree": "235", "windspeedKmph": "4", "windspeedMiles": "2" } ],  "nearest_area": [ { "areaName": [ {"value": "Randjesfontein" } ],  "country": [ {"value": "South Africa" } ], "latitude": "-25.952", "longitude": "28.143", "population": "0",  "region": [ {"value": "Gauteng" } ],  "weatherUrl": [ {"value": "http:'/'/www.worldweatheronline.com'/v2'/weather.aspx?q=-25.9484274,28.1395815" } ] } ],  "request": [ {"query": "Lat -25.95 and Lon 28.14", "type": "LatLon" } ],  "weather": [ { "astronomy": [ {"moonrise": "09:08 PM", "moonset": "08:47 AM", "sunrise": "06:45 AM", "sunset": "05:43 PM" } ], "date": "2015-08-03",  "hourly": [ {"chanceoffog": "0", "chanceoffrost": "0", "chanceofhightemp": "0", "chanceofovercast": "0", "chanceofrain": "0", "chanceofremdry": "0", "chanceofsnow": "0", "chanceofsunshine": "100", "chanceofthunder": "0", "chanceofwindy": "0", "cloudcover": "0", "DewPointC": "-3", "DewPointF": "26", "FeelsLikeC": "9", "FeelsLikeF": "48", "HeatIndexC": "9", "HeatIndexF": "47", "humidity": "43", "precipMM": "0.0", "pressure": "1028", "tempC": "9", "tempF": "47", "time": "200", "visibility": "10", "weatherCode": "113",  "weatherDesc": [ {"value": "Clear" } ]}

真的不知道我在这里做错了什么。

您可以解析JSON以获得更好的结果:

$scope.localWeather = JSON.parse(weather);

但是我用json lint检查了你的json输出,它说json是错误的。你能检查在开发控制台,如果相同的JSON来自天气服务作为你的输出?如果是,那么这是api的问题,你不能做任何事情。

Gah犯了一个愚蠢的错误,我没有考虑这个问题足够长的时间…发生错误是因为我忘记将'callback=JSON_CALLBACK'添加到jsonp呼叫的末尾…如果对其他人有帮助,我就把这个答案留在这里。